471,354 Members | 1,246 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

doing cleanup in Dispose() when implementing IDisposable

If I implement IDisposable in a custom class, how do I let the GC know
that the custom class objects aren't being used anymore, and are ready
for cleanup?

Do I just set them equal to null? Do I need to call GC.Collect() after?

Is IDisposable only meant for objects that already implement it like
sockets and stream?


Dec 6 '05 #1
6 1198
Hi Narshe,

IDisposable allows users to explicitly free unmanaged resources. You
don't need to concern about how to let the GC know it is not used any
longer nor explicitly call GC.Collect.
For information and guidelines, see


Dec 6 '05 #2
So, there really is no point in implementing IDisposable then? No need
to set the objects to null at all?

Dec 6 '05 #3
"Narshe" <na****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
So, there really is no point in implementing IDisposable then? No need
to set the objects to null at all?

If your object holds some sort of resources that you'd like the user of your
object to be able to free then you should implement dispose. For example if
your object grabbed a bitmap of the screen when it was created then you
might implement dispose so that the bitmap is destroyed.

Dec 6 '05 #4
Think of a case when your class encapsulates unmanaged resources like a
file handle. You need to close (dispose) this file handle when you
don't need it any more. The GC can't help you with unmanaged resources
since it don't know how to dispose them. The first place you could
think of to put the statement to close the file handle is the Finalize
method (in C#, use destructor). GC will call Finalize to give the
object a last chance to clean up itself before it gets collected.

However, the GC does not guarantee when it will collect a particular
piece of garbage. Thus, you should provide users with a way to
explicitly close the file handle instead of relying on the
indeterminate nature of the GC. That is where the IDisposable interface
comes into the picture. C# even provide the "using" keyword to make
using it more easy.

Guidelines on how to implement IDisposable interface could be found in
the link on my prev post.


Dec 6 '05 #5
Narshe... IDisposable is meant for _exception safe_ deterministic
cleanup of resources and is the eqivalent of RAII (Resource Acquisition
Is Initialization) in C++. It only works properly if you use it in
concert with the using syntax.

So you can either do:

Program p = null;
p = new Program();
if (p != null)
p = null;

or just:

using (Program p = new Program())



*** Sent via Developersdex http://www.developersdex.com ***
Dec 6 '05 #6
Thanks everyone.

Dec 6 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by celeong | last post: by
15 posts views Thread by Sam Sungshik Kong | last post: by
6 posts views Thread by Cody Powell | last post: by
156 posts views Thread by Dennis | last post: by
6 posts views Thread by Water Cooler v2 | last post: by
4 posts views Thread by BLUE | last post: by
3 posts views Thread by AlexS | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.