473,402 Members | 2,061 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,402 software developers and data experts.

How to dispose of ArrayList of IDisposable elements

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

May 10 '06 #1
2 10593
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#
<ch********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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

May 10 '06 #2
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.

ch********@yahoo.com wrote:
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


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200605/1
May 10 '06 #3

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

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
16
by: Cybertof | last post by:
Hi ! I'm wondering about the use of the 'using' statement. In a procedure, should i declare all instance variables in a using clause ? In the below code, what would happen if MyFont & MyFont2...
3
by: Vagabond Software | last post by:
Scenario: The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The...
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
11
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset...
16
by: Jeremy | last post by:
I was looking through some sample code that Microsoft provided for data access and I noticed that when they call the Dispose() method on the Connection object they cast it to IDisposable first. ...
7
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.