Memory: What happens to a parent when I return a child after a method executes?

A

ahaupt

Hi all,

Consider the following scenario:

main()
{

DataTableCollection dtc = GetDataColumnCollection();

}

DataColumnCollection GetDataColumnCollection()
{

//..
//Use sql data adapter to populate a new data table
//..

return dataTable.Columns;

}

What happens with the dataTable object?

I assume that the all object, which does not have a reference to them
is added to the list (??) for grabage collection.

Thus the datatablecollection should remain, since there's a reference
to it. Is this correct?

Thanks for reading,
Andre
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Yes that is what should happen, now see that you cannot be 100% sure than
dataTable will be collected, it may be possible that the DataTableColumns
instance hold a reference to the parent datatable .

Now, I'm not saying this is the case ( I do not for sure) , but that is the
answer in case you see that the dataTable is not collected.

The botton line is that any instance that has no active reference on it will
be GCed.


cheers,
 
M

Mattias Sjögren

What happens with the dataTable object?

I assume that the all object, which does not have a reference to them
is added to the list (??) for grabage collection.

The object that dataTable references becomes eligible for garbage
collection unless you hold some other reference to it (one could
imagine that the DataColumnCollection could do so internally for
example). There's no list of "garbage" objects though.

Thus the datatablecollection should remain, since there's a reference
to it. Is this correct?

Yes.



Mattias
 

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