For Programmers: Free Programming Magazines  


Home > Archive > C# .NET > April 2004 > CollectionBase Clear - Nested collections?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CollectionBase Clear - Nested collections?
Dave Veeneman

2004-04-05, 5:33 pm

Xref: kermit microsoft.public.dotnet.languages.csharp:269800

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
Chicago


Jon Skeet [C# MVP]

2004-04-05, 5:33 pm

Dave Veeneman <davidv@nospam.com> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com