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

  • Thread starter Thread starter ahaupt
  • Start date Start date
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
 
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,
 
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
 
Back
Top