Would the DataSet be released by the Garbage Collector?

S

Steven Blair

Would this DataSet be cleaned up?

void Main()
{
DataSet ds = null;

//Imagine this returns some tables
ReturnData(out ds);

//Grab a handle to the first row in the Configuration Table.
DataRow dr = ds.Tables["Configuration"].Rows[0];

//Create a new class and give the refence to the DataRow
TestClass t = new TestClass(dr);
}

Since a refence to part of the original DataSet exists, would this
prevent the DataSet being colelcted by the Garbage collector?

I know I could extract the fields from the Row, into another container,
but the DataRow really is the object I need to use.

Thanks for the help in advance.

Steven
 
S

Stefan Hoffmann

hi Steven,

Steven said:
Would this DataSet be cleaned up?
Yes, if ReturnData() is side-effect free. All your other
objects/references are local to your Main() methode.


mfG
--> stefan <--
 
S

Steven Blair

What I mean though, if TestClass kicked of Threads and control was no
returned to the main (the application is a service).

Would the DataSet be collected at at some point during the lifetime of
the service, or would the DataSet remain in memory because my TestClass
still has a reference back to the DataSet.
 
D

Dave Sexton

Hi,

Well, DataRow itself has a reference to it's DataTable, which has a
reference to it's DataSet. So even though the local variables are out of
scope, the GC can't collect the DataSet since "live" objects still reference
it.
 
D

Dave Sexton

Hi,

Keeping a reference to the DataRow will prevent the GC from collecting the
DataSet since it's still "in use".
 
S

Steven Blair

Dave,

Thanks, that what I thought.
I might have to take a copy of the DataRow or hold the information in a
different way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top