CollectionBase Clear - Nested collections?

  • Thread starter Thread starter Dave Veeneman
  • Start date Start date
D

Dave Veeneman

I have created a collection class derived from CollectionBase that can be
nested several layers deep. so my root object has several child objects,
each of which has several grandchild objects, and so on. (The structure is a
GoF Composite.)

If I call the CollectionBase.Clear method on the root object, will it
release all child and grandchild objects for garbage collection, or will it
release only the root-level children. In other words, do I need to call
Clear just on the root object, or do I need to traverse the structure and
call Clear on each object in the structure? Thanks.
 
Dave Veeneman said:
I have created a collection class derived from CollectionBase that can be
nested several layers deep. so my root object has several child objects,
each of which has several grandchild objects, and so on. (The structure is a
GoF Composite.)

If I call the CollectionBase.Clear method on the root object, will it
release all child and grandchild objects for garbage collection, or will it
release only the root-level children. In other words, do I need to call
Clear just on the root object, or do I need to traverse the structure and
call Clear on each object in the structure? Thanks.

Objects are eligible for garbage collection if there are no more
reachable references to them. If the only references to the
"grandfather" objects were the ones in the CollectionBase, then
clearing the CollectionBase will effectively make the whole tree of
objects orphaned, and they'll be eligible for garbage collection. Of
course, if any of those objects are referenced elsewhere, those
particular objects (and any objects they reference in turn) won't be
garbage collected.
 
Back
Top