469,601 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

How to Create Your Own Dispose method

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.

--
Siv
Martley, Near Worcester, UK.
Nov 23 '05 #1
5 1959
"Siv" <ms**********@removeme.sivill.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #2
Siv
Herfried,
Thanks for this.

--
Siv
Martley, Near Worcester, UK.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
"Siv" <ms**********@removeme.sivill.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #3
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

Siv wrote:
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.

--
Siv
Martley, Near Worcester, UK.


Nov 23 '05 #4
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/de...classtopic.asp

Cor
Nov 23 '05 #5
Cor Ligthert [MVP] wrote:
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.
* 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.


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.
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/de...classtopic.asp

Cor


Nov 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by SamIAm | last post: by
1 post views Thread by Billy | last post: by
3 posts views Thread by Richard Skopal | last post: by
6 posts views Thread by Teresa | last post: by
11 posts views Thread by Mark B | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.