Agressive disposing

E

Elisa

Hi,

As Pocket PC's are (supposed to be) resource-restricted devices, I
thought it would be wise to implement an agressive disposing strategy,
i.e. add IDisposable to every class, and call Dispose() on everything.

But I don't know exactly HOW agressive I need to be ;-)

I.e.:

- If I derive from Panel or Form, and a child controls
(Me.Controls.Add...), do I need to remove and dispose those as well, or
do Panels and Forms somehow dispose of their children themselves?
- If I add objects to a Collection, do I need to enumerate over the
collection and call Dispose() on every object, or should a
Collection.Clear() accomplish the same thing?
- If I add a handler (AddHandler obj.event, delegate), do I need to
remove the handler myself when I want to dispose of the object?

I assumed that all the above would be taken care of automatically by the
garbage collector, but some of my objects start threads, and these
threads are stopped in their Dispose() method (via a simple
stopThreadRequested boolean flag). Yet if I quit my application, these
threads appear to still be running, so it looks as if my Dispose()
methods aren't called automatically...


Regards,

Elisa
 
W

William Ryan eMVP

Elisa:

Technically I would need to see all of the objects before advising against
implementing Dispose on each of them, but it's a very good bet that you
don't need to do this. Notice that every object in .NET doesn't have a
Dispose method.... so chances are that if there's not a need to explicit
dispose method in all of the framework objects, that there's not such a need
with yours.. It is true that PDA's have more limited resources and that you
want to release resources as early as possible (on any environment, but
particularly with a PDA).

Now, when something is garbage collected, it releases all managed resources
which that object consumes. BUT, and this is a big but, there are many
things that the GC doesn't know how to get rid of. These are primarily the
cases where you want to implement IDisposable...things like Database
connections, open file streams, windows handles etc.

There are many other issues that will affect when things go away and
resource consumption and this is but one of many.

HTH,
Bill
 
G

Geoff Schwab [MSFT]

Hi Elisa,

If you have a C++ background like me then this can be a hard concept to get
used to but I would actually recommend only adding Dispose methods to those
objects that either allocate resources or open some sort of connection that
needs to be closed, e.g., streams. If you want to be agressive about GC
then it should suffice just to set objects to null if they do not have a
Dispose method. It is also good practice to, when possible, use objects
that have Dispose within a try/finally branch. For example...

MyBitmap myBmp = null; // MyBitmap has a Dispose method
try
{
Bitmap bmp = new Bitmap(100,100);
...
// Now I am done with it
bmp = null;

// Do some stuff with myBMP
}
finally
{
if (myBmp != null)
myBmp.Dispose();
}

To answer your question about collections, you will need to Dispose any
objects that require it. If you want to get tricky then you can write a
method that uses Reflection to determine if an object has a Dispose method
and calls it. You can then call this method on every object before
releasing it.

Hopefully I did not just confuse you more.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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