Home All Groups Group Topic Archive Search About

Question about Destructors

Author
16 Apr 2005 10:47 AM
kd
Hi All,

When should one use the Finalize method and when the Dispose method?

kd

Author
16 Apr 2005 11:13 AM
Herfried K. Wagner [MVP]
"kd" <k*@discussions.microsoft.com> schrieb:
> When should one use the Finalize method and when the Dispose method?

..NET Framework Developer's Guide -- Programming for Garbage Collection
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconprogrammingessentialsforgarbagecollection.asp>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
16 Apr 2005 12:07 PM
Cor Ligthert
KD,

> When should one use the Finalize method and when the Dispose method?

One of the advantages from managed code is that it does what it says it is
for. It manages your code and especially in that the removing of used
objects.

So the best approach is not doing both as long as you uses the dotnet
classes (with some small exceptions however when it is not written than
forget those).

When you start building your own classes from scratch than you have to watch
to use the Idisposable interface when you are using unmanaged resources
(dont mix that up with managed code).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIDisposableClassTopic.asp

Because that most classes we use are derived from the component class which
implements this, is this mostly implemented.

The best what I have read (although probably as well still incomplete is
this)

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/3e55c043b1e6ae59

(Impliciet in this text is when it is by instance a class that is created
using the form or the component designer, than you see it in that).

I hope this helps,

Cor
Author
17 Apr 2005 11:13 AM
Adam Goossens
Hi kd,

Implement IDisposable and use Finalize() when you're holding onto *any*
unmanaged resources that you need to persist across the lifetime of your
object. Database connections are a big one - create a connection in the
constructor, stuff it into a member and reference it when you need it.
But you have to close it sometime! :)

You'll have to check out the resources provided by Cor and Herfried -
explaining the intricacies in any great detail would take a lot of
writing. Suffice it to say that unmanaged code cleanup goes into
Dispose(), and a call to Dispose() goes into Finalize().

This ensures that unmanaged resources are released - either manually by
the programmer (which is the *best* way), or automatically when the
object undergoes garbage collection.

Use GC.SuppressFinalize(Me) in Dispose() to prevent your destructor from
being executed. This is necessary to ensure that you don't attempt to
call Dispose() twice and hence free the same resources twice!

Regards,
-Adam.

kd wrote:
Show quoteHide quote
> Hi All,
>
> When should one use the Finalize method and when the Dispose method?
>
> kd
Author
17 Apr 2005 11:32 AM
Adam Goossens
Oh, I should point out this is just my POV. It's really a kind of holy
war on this sort of thing :)