473,440 Members | 1,843 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,440 software developers and data experts.

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 2080
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
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...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
3
by: Richard Skopal | last post by:
In .NET Windows forms I can create a metafile using this code: Graphics grph = aControl.CreateGraphics(); IntPtr ipHDC = grph.GetHdc(); Metafile mf = new Metafile(aImgFilePath, ipHDC,...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
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...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.