How to dispose of ArrayList of IDisposable elements

C

chrisaotto

I have a three part question concerning the proper use of Dispose --
specifically when I have a collection of elements (contianed in an
ArrayList) that each implement the IDisposable interface.

First, if my class declares disposable members, then I must also
implement IDispoable. Is thsi correct? In my case, I don't really
have any disposable members though - I have an ArrayList of disposable
members. But since my class is creatign and managing these objects, it
seems I must dispose them. Is this correct?

Second, In my class's Dispose method, can I simply 'clear' the
ArrayList or must I Dispose each element separately?

Finally, what should I do when my class is not being disposed, but
simply [permanently] removing an object from the ArrayList. Is it
sufficient to simply remove all handles and let it be collected in
normal fashion? Or must I explicitly Dispose each object when I am
done with it.

Thank you!
Chris
 
G

Greg Young

It depends ... are these objects private to your class or is your class
considerred the primary container for them (i.e. if they are no longer in
the context of your class they are useless?).

If they are private (i.e. composite) then you most likely should be
disposing of them when you are done with them. If you are allowing others
access to them then you should not as someone may still have a reference to
the object. A noticable exception to the second case occurs in Windows.Forms
... when a form is disposed, all of its child controls are disposed yet you
can get a reference to them externally (thus you run the possibility of
holding a reference to a disposed control).

Cheers,

Greg Young
MVP - C#
 
S

smc750 via DotNetMonster.com

I would not just clear the array. I would loop through the array and call the
dispose method on each object.
If the objects hold on to some resource then you must do this to release it.
Otherwise it may take a while for the resources to be reclaimed via garbage
collection.

Hope this helps.

I have a three part question concerning the proper use of Dispose --
specifically when I have a collection of elements (contianed in an
ArrayList) that each implement the IDisposable interface.

First, if my class declares disposable members, then I must also
implement IDispoable. Is thsi correct? In my case, I don't really
have any disposable members though - I have an ArrayList of disposable
members. But since my class is creatign and managing these objects, it
seems I must dispose them. Is this correct?

Second, In my class's Dispose method, can I simply 'clear' the
ArrayList or must I Dispose each element separately?

Finally, what should I do when my class is not being disposed, but
simply [permanently] removing an object from the ArrayList. Is it
sufficient to simply remove all handles and let it be collected in
normal fashion? Or must I explicitly Dispose each object when I am
done with it.

Thank you!
Chris
 

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