473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Disposing object is a collection

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 20 '05 #1
4 1144
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 20 '05 #2
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 20 '05 #3
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 20 '05 #4
"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 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MC D | last post by:
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...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
4
by: gmccallum | last post by:
This whole dispose topic confuses me regarding managed and unmanaged resources. I think that it was easier when you just cleaned up every object, which brings me to my problem. I have some...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.