473,851 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sub Dispose or Implementing the Dispose Interface?

In a typical class, do I need to indicate that it implements the IDisposable
interface and then create a Dispose method that implements the Dispose
required by the IDisposable interface or can I just make a Sub Dispose() and
the CLR will know that this is the Dispose method to call when the object
falls out of scope?

Thanks.
Nov 21 '05 #1
7 2100
The runtime (CLR) never calls the dispose method. It is upto the client
using the object to call dispose. There are a couple of exceptions. Read
this for more information:
http://blogs.msdn.com/clyon/archive/...21/232445.aspx

So whether you just add a Dispose method or implement IDisposable interface,
its upto the application using your class to call the dispose method.
Ofcourse, it makes more sense to implement IDisposable since that is indeed
why the interface is there - for one to write dispose code. If you simply
write a Sub Dispose, it could be misleading to someone using your class
since one would expect an implementation of IDisposable if one were to
follow the standards.
hope that helps..
Imran.

"Scott M." <No****@NoSpam. com> wrote in message
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements the
Dispose required by the IDisposable interface or can I just make a Sub
Dispose() and the CLR will know that this is the Dispose method to call
when the object falls out of scope?

Thanks.

Nov 21 '05 #2
Scott,
The framework itself never calls Dispose.

C# will call IDisposable.Dis pose if you use the object in a Using statement.

VB.NET 2002 & 2003 & C#'s For Each statements will call IDisposable.Dis pose
if its part of a class that implements IEnumerator.

Otherwise VB.NET (2002 & 2003) will not call IDisposable.Dis pose.

VB.NET 2005 (aka Whidbey, due out later in 2005) will add support to VB.NET
for the Using statement, the Using statement will call IDisposable.Dis pose
on the End Using line.

Information on VB.NET 2005:
http://lab.msdn.microsoft.com/vs2005/

Information on VB.NET's Using statement:
http://msdn2.microsoft.com/library/htd05whh.aspx

An object falling out of scope never calls Dispose nor IDisposable.Dis pose.
If you are considering implementing IDisposable, you should also consider
implementing Finalize also.

The "Implementi ng Finalize and Dispose to Clean Up Unmanaged Resources" on
MSDN:

http://msdn.microsoft.com/library/de...izeDispose.asp

Covers when you should implement Finalize, when you should implement both,
and when you should implement just IDisposable.

Hope this helps
Jay

"Scott M." <No****@NoSpam. com> wrote in message
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements the
Dispose required by the IDisposable interface or can I just make a Sub
Dispose() and the CLR will know that this is the Dispose method to call
when the object falls out of scope?

Thanks.

Nov 21 '05 #3
Ok Imran, then let me ask you this...

When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object that
is no longer referenced are the ones collected?
"Imran Koradia" <no****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
The runtime (CLR) never calls the dispose method. It is upto the client
using the object to call dispose. There are a couple of exceptions. Read
this for more information:
http://blogs.msdn.com/clyon/archive/...21/232445.aspx

So whether you just add a Dispose method or implement IDisposable
interface, its upto the application using your class to call the dispose
method. Ofcourse, it makes more sense to implement IDisposable since that
is indeed why the interface is there - for one to write dispose code. If
you simply write a Sub Dispose, it could be misleading to someone using
your class since one would expect an implementation of IDisposable if one
were to follow the standards.
hope that helps..
Imran.

"Scott M." <No****@NoSpam. com> wrote in message
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements
the Dispose required by the IDisposable interface or can I just make a
Sub Dispose() and the CLR will know that this is the Dispose method to
call when the object falls out of scope?

Thanks.


Nov 21 '05 #4
Scott,

When you use the componenttempla te to create the class, it gives you a
complete class where IDisposable setting is completly done for you.

In any class that implements IDisposable is the disposing from the resourses
used in that class done for you.

Cor
"Scott M." <No****@NoSpam. com> schreef in bericht
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements the
Dispose required by the IDisposable interface or can I just make a Sub
Dispose() and the CLR will know that this is the Dispose method to call
when the object falls out of scope?

Thanks.

Nov 21 '05 #5

"Scott M." <No****@NoSpam. com> wrote
Ok Imran, then let me ask you this...

When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object that
is no longer referenced are the ones collected?

Its not quite that simple. The GC is good at cleaning up short lived objects,
but those that hang around for a while get shoved on 'the back burner' (meaning
they don't get tested as often).

The GC is also pretty good at managing those long lived objects, but its the
middle ground that needs to be addressed. That area is dependant on your
own use of memory, so just how often the GC runs, and decides to move
things to the 'back burner' is partially dependant on what you're doing....

FWIW:
The proper term is 'generations'. Generation 0 items come and go as needed....

So, its those Gen 0 items that are swept away when nothing holds a reference
to them. If they survive that first look, then get shoved into the Gen 1 area, and
then Gen 2, etc where they don't go looking for memory as often as the Gen 0 area.

For a bit more on the GC, you might want to watch the .Net show where one
of the CLR's architect was interviewed:
http://msdn.microsoft.com/theshow/Ep...27/default.asp

LFS

Nov 21 '05 #6
Scott,

As Larry mentioned, collection is a bit more complicated. I think you are
confusing a finalize method with the dispose method (I could be wrong
though). If an object has a finalizer, it is not collected (when a
collection is performed by the GC and it finds that the object is not
referenced anymore) but instead is pushed onto a finalization queue so that
the runtime can execute the finalize method of the object. And then there is
the concept of various generations (0, 1 and 2 in the current framework
version). I would suggest you read these 2 excellent articles by Jeffrey
Richter on Garbage collection which also talks about finalization and
resurrection:
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/
http://msdn.microsoft.com/msdnmag/is...2/default.aspx

Also, here is a blog post on dispose dos and don'ts by Chris Lyon which you
might be interested in since you are deciding to implement dispose in your
class:
http://blogs.msdn.com/clyon/archive/...23/233464.aspx
hope that helps..
Imran.

"Scott M." <No****@NoSpam. com> wrote in message
news:Og******** ******@TK2MSFTN GP12.phx.gbl...
Ok Imran, then let me ask you this...

When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object
that is no longer referenced are the ones collected?
"Imran Koradia" <no****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
The runtime (CLR) never calls the dispose method. It is upto the client
using the object to call dispose. There are a couple of exceptions. Read
this for more information:
http://blogs.msdn.com/clyon/archive/...21/232445.aspx

So whether you just add a Dispose method or implement IDisposable
interface, its upto the application using your class to call the dispose
method. Ofcourse, it makes more sense to implement IDisposable since that
is indeed why the interface is there - for one to write dispose code. If
you simply write a Sub Dispose, it could be misleading to someone using
your class since one would expect an implementation of IDisposable if one
were to follow the standards.
hope that helps..
Imran.

"Scott M." <No****@NoSpam. com> wrote in message
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements
the Dispose required by the IDisposable interface or can I just make a
Sub Dispose() and the CLR will know that this is the Dispose method to
call when the object falls out of scope?

Thanks.



Nov 21 '05 #7
Yes, I know that Cor. My question was about classes that are built from
scratch.
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:eC******** ******@TK2MSFTN GP10.phx.gbl...
Scott,

When you use the componenttempla te to create the class, it gives you a
complete class where IDisposable setting is completly done for you.

In any class that implements IDisposable is the disposing from the
resourses used in that class done for you.

Cor
"Scott M." <No****@NoSpam. com> schreef in bericht
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
In a typical class, do I need to indicate that it implements the
IDisposable interface and then create a Dispose method that implements
the Dispose required by the IDisposable interface or can I just make a
Sub Dispose() and the CLR will know that this is the Dispose method to
call when the object falls out of scope?

Thanks.


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5334
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
24
7711
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
6
2163
by: TB | last post by:
I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. My question is to what extent can I rely on access to my private data during the finalization process when *not* coming through the IDisposable interface? Are private members still valid or are they managed objects that may already be gone? Are some types accessible...
7
4471
by: Tamir Khason | last post by:
I have a class public class Foo { public Foo() { DoSomething() } } I want to be able to do something else while the class enters GC - no more
4
2008
by: Joey | last post by:
Hi, I have come across codes like this if(dbConn != null) { dbConn.dispose(); } and sometimes like
3
2083
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will happen if I just create my own Dispose (or any other name) mehtod, without implementing the IDisposable interface. In all the examples, which I found, this Dispose method called from my user code (not by GC). What do I miss? Thanks.
6
1837
by: Cody Powell | last post by:
Greetings all, I'm wondering if there's a rule of thumb related to objects that implement IDisposable, but whose Dispose() methods are actually marked as protected. If you dig around System.IO, you'll see a lot of these, like StreamReader, StreamWriter, etc. Is it still a best practice to wrap those objects in a using block? All of the MSDN docs DO wrap such objects in a using block, but I'm wondering if that actually accomplishes...
71
10786
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 is that automatic? Thanks
1
1215
by: Piotrekk | last post by:
Hi I have few questions which I would like to ask: 1. When would I place my own code to Form1 : Form method 2. When would I create Dispose method by implementing IDispose - what is the example scenario. If so, why is better than doing something different / simpler What I am trying to understand is the reason for IDisposable
0
9747
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11017
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10728
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9505
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7905
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.