1

I see a lot of code where the objects are set to null in the finally section or at the end of a method, for example:

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void buildUpdates(int batchId, string mkt)
        {
            aplUpdate oDU;
            DataSet ds;
            DataTable dtTbls;
            DataTable dtData;
            List<dbVer> resp;

            try
            { 
                oDU = new WEB_APL.APL.aplUpdate(MyWeb.Properties.Settings.Default.DBCon);

                ds = new DataSet(); 
                ds.DataSetName = batchId.ToString();
                dtTbls = oDU.getDetails(true);
                resp=GetDBVersions();

                foreach (DataRow dr in dtTbls.Rows)
                {
                    .....              
                }
                ds.WriteXml(HttpContext.Current.Server.MapPath("~") + "\\" + ds.DataSetName + ".xml", XmlWriteMode.WriteSchema);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
               oDU = null;
               ds=null;
               rest=null;
               dtTbls=null;
               dtData=null;
            }
        }

I think this is unnessesary since expired objects will be handled automatically by the .NET garbage collector.

Is there a reason why one should reset to null?

1

3 Answers 3

2

Actually it is not handled automatically.

The issue being that as long as for example oDU is referenced, it can not be collected. Which means that basically if your holding object is not collected then references can not be collected. GOod style is to clear references when you do not need them anymore. At least for class / struct level variables.

Now, in this particular case it is totally not needed. The GC has nothing to do with this.

What has to do with this is that all these variables are local to the method, so they go out of scope anyway at the end of the method (and in runtime earlier, once they fall out of scope).

As such it is simply totally redundant.

Sign up to request clarification or add additional context in comments.

2 Comments

So once they are out of scope they will be cleaned up by the GC when it feels to do so?
Yes. As they are not referenced at all the GC will collect them - eventually. Core here is "when it feels to do so".
1

If they are local variables, no, it does not make sense. It's probably a legacy habit.

If they are class level variables, it might make sense. Depends on the use.

Edit:

        catch (Exception ex)
        {
            throw ex;
        }

This however is a faux-pas. This will cripple the stacktrace. If you don't want to handle an exception, don't use a catch block or use the throw keyword without the variable so the stacktrace is kept intact.

        catch (Exception ex)
        {
            throw;
        }

2 Comments

Thanks for noting that throw ex.
Hey @nvoigt If you enjoy doing such style suggestions and similar, how about you give Code Review a visit? I'm sure you'll like it :)
1

This is probably intended to prevent memory leaks and unneeded resource locks on things like:

  • Database Connections
  • Files
  • Streams
  • Generally Lockable and Disposable Objects

THIS DOES NOT WORK
Setting Objects (especially connections and streams) to null does not cleanly dispose/close them, in fact it's totally useless noise in your code.

you should instead use using statements, similar to the following example:

using (IDisposable thingy = new FileStream("filepath/connection/whatever"){
   //do something with thingy
}

1 Comment

Just that it does not prevent them. It's simply superfluous. Using-blocks would indeed help, setting it to null is... dead code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.