473,320 Members | 2,048 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,320 software developers and data experts.

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 2071
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**************@TK2MSFTNGP14.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.Dispose if you use the object in a Using statement.

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

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

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.Dispose
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.Dispose.
If you are considering implementing IDisposable, you should also consider
implementing Finalize also.

The "Implementing 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**************@TK2MSFTNGP14.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****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.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**************@TK2MSFTNGP14.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 componenttemplate 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**************@TK2MSFTNGP14.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**************@TK2MSFTNGP12.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****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.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**************@TK2MSFTNGP14.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**************@TK2MSFTNGP10.phx.gbl...
Scott,

When you use the componenttemplate 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**************@TK2MSFTNGP14.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
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...
24
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...
6
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...
7
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
by: Joey | last post by:
Hi, I have come across codes like this if(dbConn != null) { dbConn.dispose(); } and sometimes like
3
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...
6
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,...
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.