473,320 Members | 2,189 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,320 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 15 '05 #1
4 1460
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Egghead | last post by:
Hi all, I'm just wondering that is any way to dispose all objects when my app is shut down? I mean I can use for.. next loop to unload all loaded form when I shut down my app. Thank you, ...
0
by: TP-Software | last post by:
Hi, I have a ChatFormCollection class which holds a collection of the class ChatForm which inherits from Form. MainForm has an instance of ChatFormCollection. When a ChatForm instance is...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
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...
7
by: Mariusz | last post by:
Hello, I have question regarding disposing objects in aspx/cs pages. If I create some objects/Arrays/ListArrays in pageLoad do I have to dispose them (by assigning the null value for objects that...
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...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.