Disposing object is a collection

M

MC D

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
 
F

Fergus Cooney

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
 
M

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?

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 said:
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
 
F

Fergus Cooney

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
 
A

Alan Pretre

MC D said:
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
 

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