Dispose... GC

  • Thread starter Thread starter vj
  • Start date Start date
V

vj

I have the below method

private DataSet ReadNCFFile(string NCFFileName)
{
DataSet dsNCFFile = new DataSet();

try
{
dsNCFFile.ReadXml(NCFFileName);
return dsNCFFile.Copy();
}
finally
{

}
}

private void CallingMethod()
{
DataSet ds = ReadNCFFile(strFile);
}

will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(),
rather than dsNCFFile. I am assuming here that in the calling method, the
"ds" will receive a copy and hence the reference to the DataSet in
ReadNCFFile will be lost, so GC will garbage collect it soon??? , or I am
totally way of here?

Any ideas??

Vijay
 
vj said:
I have the below method

private DataSet ReadNCFFile(string NCFFileName)
{
DataSet dsNCFFile = new DataSet();

try
{
dsNCFFile.ReadXml(NCFFileName);
return dsNCFFile.Copy();
}
finally
{

}
}

private void CallingMethod()
{
DataSet ds = ReadNCFFile(strFile);
}

will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(),
rather than dsNCFFile. I am assuming here that in the calling method, the
"ds" will receive a copy and hence the reference to the DataSet in
ReadNCFFile will be lost, so GC will garbage collect it soon??? , or I am
totally way of here?

The return value of ReadNCFFile is a reference to the object created in
the method. Therefore it won't be garbage collected - you've still got
a reference to it in CallingMethod.

The variable ds is a copy of the *reference*, not a copy of the
*object*.
 
vj said:
I have the below method

private DataSet ReadNCFFile(string NCFFileName)
{
DataSet dsNCFFile = new DataSet();

instance A
try
{
dsNCFFile.ReadXml(NCFFileName);
return dsNCFFile.Copy();

returns new instance B. A is available for GC
}
finally
{

}
}

private void CallingMethod()
{
DataSet ds = ReadNCFFile(strFile);
}

will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(),
rather than dsNCFFile.

Instance A will be available for GC after return but you cannot pin down
"soon".
I am assuming here that in the calling method, the "ds" will receive a
copy and hence the reference to the DataSet in ReadNCFFile will be lost,
so GC will garbage collect it soon??? , or I am totally way of here?

ds refers to instance B - the one returned by Copy()

B is an exact copy of A which leads one to question why DataSet doesn't
implement ICloneable and call it Clone()!

Even more confusingly DataSet has a Clone() method which does not clone it
contrary to all reasonable expectation! It just copies the schema. IMHO this
is a horrible MS error.

Ultimately what you are doing is a pointless waste of CPU - just return the
original instance
 
I missed that reference part... Thanks Jon

oh gosh.. even Clone does not work... very interesting.... so GC will not
garbage collect...really bad miss by MS

VJ
 
vj said:
I missed that reference part... Thanks Jon

oh gosh.. even Clone does not work... very interesting.... so GC will not
garbage collect...really bad miss by MS

GC WILL collect ALL garbage (i.e. instance A).

You can't just come up with your own personal definition of garbage and then
complain that the GC doesn't collect it.

Also Clone() DOES work exactly as documented. The problem is not that it
doesn't work but that almost everywhere else Clone is the implementation of
ICloneable and does what it says there whereas here it is NOT implementing
ICloneable and does something different. It is not strictly wrong - it is
just very confusingly named.
 
Back
Top