By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

Disposing object is a collection

P: n/a
Question:

If I have a class which has a property which is a collection of widget
objects (an arrayList of widgets), and both the containter class and the
widget class implement the IDisposable interface, does the container class
need to call the dispose method of each of the widgets when its dispose
method is called, or does this happen automatically because the container
class will go "out of scope"?

Thanks for any help!

-D
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi MC D,

If the Collection is the only owner of these objects, I'd say Dispose of them explicitly. But if there may be other
references floating around, you would be applying a knee to their balls! Ouch.

It not done, it will happen automatically when the GC can be bothered, so to speak.

Regards,
Fergus
Nov 15 '05 #2

P: n/a
I guess part of my question is, if each of these is saying they implement
IDisposable, then is the framework "smart" enough to know that any reference
in an object to another object that implements Idisposeable is automatically
disposed of when its parent calls its own dispose method. Make sense?

Fergus makes a good point about external references to items in the
collection... I dunno... how is this usually handled? There is nothing
keeping someone for making a reference to an item in the objects collection,
then disposing the "parent" object... but that would seem pretty stupid... I
mean, If I'm going to dispose the object that gave me the reference in the
first place, I'd expect any other references to that objects members to go
bye-bye too!

-D

"Fergus Cooney" <fi****@post.com> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
Hi MC D,

If the Collection is the only owner of these objects, I'd say Dispose of them explicitly. But if there may be other references floating around, you would be applying a knee to their balls! Ouch.
It not done, it will happen automatically when the GC can be bothered, so to speak.
Regards,
Fergus

Nov 15 '05 #3

P: n/a
Hi MC D,

|| I guess part of my question is, if each of these is saying they
|| implement IDisposable, then is the framework "smart" enough
|| to know that any reference in an object to another object that
|| implements Idisposeable is automatically disposed of when its
|| parent calls its own dispose method. Make sense?

Yep, makes sense and no, for the reason hinted at before. Within the Framework, only the GC knows who and how many,
when it comes to references. And at that point the question's past its sell-by date. If a class knows that it <should> be
the only one with references to its Widgets then it should Dispose when possible - if there are other refs in such a case,
let them get their Exception, it's deserved. But if others <may> have refs, leave well alone.

|| There is nothing keeping someone for making a reference to an
|| item in the objects collection, then disposing the "parent" object...
|| but that would seem pretty stupid

I can imagine wanting to keep a few TreeNodes but getting rid of the Tree, for instance. I think it's like Clone - is
it deep or is it shallow? Well, it's up to the designer - they'll have 'guessed/decided' which seems most reasonable.
Likewise Dispose - shallow or deep? The answer has to be, "well, it depends".... I would expect that more often than not,
Dispose is shallow. It would be up to the user of the container to iterate and Dispose of the contents and then finally
Dispose of the container, too.

Regards,
Fergus
Nov 15 '05 #4

P: n/a
"MC D" <as***@earthtalk.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If I have a class which has a property which is a collection of widget
objects (an arrayList of widgets), and both the containter class and the
widget class implement the IDisposable interface, does the container class
need to call the dispose method of each of the widgets when its dispose
method is called, or does this happen automatically because the container
class will go "out of scope"?


ArrayList in and of itself does not implement IDisposable. You could derive
your own class from ArrayList and implement IDisposable there, then build
the intelligence into it to dispose its members when the container gets
disposed.

-- Alan
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.