Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Sub Dispose or Implementing the Dispose Interface?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott M. - 21 Oct 2004 03:14 GMT
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.
Imran Koradia - 21 Oct 2004 04:22 GMT
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/2004/09/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.

> In a typical class, do I need to indicate that it implements the
> IDisposable interface and then create a Dispose method that implements the
[quoted text clipped - 3 lines]
>
> Thanks.
Scott M. - 21 Oct 2004 05:31 GMT
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?

> 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
[quoted text clipped - 19 lines]
>>
>> Thanks.
Larry Serflaten - 21 Oct 2004 09:59 GMT
"Scott M." <NoSpam@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/Episode027/default.asp

LFS
Imran Koradia - 21 Oct 2004 13:00 GMT
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/issues/1200/GCI2/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/2004/09/23/233464.aspx

hope that helps..
Imran.

> Ok Imran, then let me ask you this...
>
[quoted text clipped - 25 lines]
>>>
>>> Thanks.
Jay B. Harlow [MVP - Outlook] - 21 Oct 2004 04:31 GMT
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/default.asp?url=/library/en-us/cpgenref/html/c
pconFinalizeDispose.asp


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

Hope this helps
Jay

> In a typical class, do I need to indicate that it implements the
> IDisposable interface and then create a Dispose method that implements the
[quoted text clipped - 3 lines]
>
> Thanks.
Cor Ligthert - 21 Oct 2004 09:09 GMT
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

> In a typical class, do I need to indicate that it implements the
> IDisposable interface and then create a Dispose method that implements the
[quoted text clipped - 3 lines]
>
> Thanks.
Scott M. - 21 Oct 2004 23:52 GMT
Yes, I know that Cor.  My question was about classes that are built from
scratch.

> Scott,
>
[quoted text clipped - 13 lines]
>>
>> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.