472,791 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 2058
"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,...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.