Home All Groups Group Topic Archive Search About
Author
18 Feb 2006 12:24 AM
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

Author
18 Feb 2006 7:16 AM
Jon Skeet [C# MVP]
vj <vijayba***@yahoo.com> wrote:
Show quote
> 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*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
18 Feb 2006 12:41 PM
Nick Hounsome
"vj" <vijayba***@yahoo.com> wrote in message
news:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl...
>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

Show quote
>       }
>       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
Author
20 Feb 2006 5:29 PM
vj
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


Show quote
"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:IBEJf.19909$Q22.16575@fe1.news.blueyonder.co.uk...
>
> "vj" <vijayba***@yahoo.com> wrote in message
> news:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl...
>>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
>
>
Author
21 Feb 2006 1:00 PM
Nick Hounsome
"vj" <vijayba***@yahoo.com> wrote in message
news:un$e7MkNGHA.1760@TK2MSFTNGP10.phx.gbl...
>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.


Show quote
>
> VJ
>
>
> "Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
> news:IBEJf.19909$Q22.16575@fe1.news.blueyonder.co.uk...
>>
>> "vj" <vijayba***@yahoo.com> wrote in message
>> news:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl...
>>>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
>>
>>
>
>

AddThis Social Bookmark Button