How to Create Your Own Dispose method

S

Siv

Hi,
I have a class module that I have created and I have looked at the various
descriptions of how you should implement a dispose method in your own class
and I am finding it mighty confusing. Does anyone have an example of the
correct way to implement a dispose method or a link to an understandable
site that has this information.

Thanks for your help.
 
H

Herfried K. Wagner [MVP]

Siv said:
I have a class module that I have created and I have looked at the various
descriptions of how you should implement a dispose method in your own
class and I am finding it mighty confusing. Does anyone have an example
of the correct way to implement a dispose method or a link to an
understandable site that has this information.

..NET Framework Developer's Guide -- Implementing a 'Dispose' Method
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconimplementingdisposemethod.asp>

INFO: Implementing 'Dispose' Method in a Derived Class
<URL:http://support.microsoft.com/?scid=kb;EN-US;315528>
 
B

Brian Gideon

In addition to the links Herfried provided I'd like to point out some
of the rules I use when deciding to implement IDisposable and
overridding Finalize. It's not clearly indicated in the MSDN articles.

* DO implement IDisposable if your class directly uses unmanaged
resources.

* DO implement IDisposable if your class indirectly uses unmanaged
resources. For example, if your class has a reference to another
object that implements IDisposable.

* DO override the Finalize method if your class directly uses unmanaged
resources.

* DON'T override the Finalize method if your class does not directly
use managed resource. In other words, don't override Finalize just
because your class has a reference to another class that implements
IDisposable.

* DON'T override the Finalize method in a class who's base class
already overrides Finalize. In other words, if the base class provides
the protected Dispose(disposing as Boolean) method then you don't need
to override Finalize.

* DON'T implement IDisposable if your class does not retain unmanaged
resources either directly or indirectly. In other words, don't
implement IDisposable just because a method on your class has a local
reference to a class that implements IDisposable or because that method
uses unmanaged resources locally. Just make sure that the method calls
Dispose on all local objects that implement IDisposable and releases
all unmanaged resources that are local to the method.

Brian
 
C

Cor Ligthert [MVP]

Brian,

Why are you in my opinion consequently giving advices that are direct
against what is written on MSDN?

Especially this one.
* DO implement IDisposable if your class indirectly uses unmanaged
resources. For example, if your class has a reference to another
object that implements IDisposable.

The garbage collector automatically releases the memory allocated to a
managed object when that object is no longer used, however, it is
unpredictable when garbage collection will occur. Furthermore, the garbage
collector has no knowledge of unmanaged resources such as window handles,
and open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged
resources in conjunction with the garbage collector. The consumer of an
object can call this method when the object is no longer needed.

It is a version breaking change to add the IDisposable interface to an
existing class, as it changes the semantics of the class.

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemidisposableclasstopic.asp

Cor
 
B

Brian Gideon

Cor said:
Brian,

Why are you in my opinion consequently giving advices that are direct
against what is written on MSDN?

I haven't seen that written anywhere. The article you cited doesn't
mention that.
Especially this one.

The garbage collector automatically releases the memory allocated to a
managed object when that object is no longer used, however, it is
unpredictable when garbage collection will occur. Furthermore, the garbage
collector has no knowledge of unmanaged resources such as window handles,
and open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged
resources in conjunction with the garbage collector. The consumer of an
object can call this method when the object is no longer needed.

Exactly. If my class has a reference to another class that implements
IDisposable then it is safe to assume that my class implicitly uses
unmanaged resources. By calling Dispose on my class I *explicitly*
release the unmanaged resources that I *implicitly* held.
 

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

Similar Threads


Top