Finalizer vs. Dispose

  • Thread starter Klaus Aschenbrenner
  • Start date
J

Jochen Kalmbach

Hi Klaus Aschenbrenner,
When should we use a finalizer and when should we implement the
IDisposable interface? I think when we have the IDisposable interface
we don't need a finalizer - or are there any scenarios when a
finalizer is better than implementing the IDisposable interface?

Finalizer and IDisposable are primarily two different things...

If a class implements a finalizer, it will (mostly) be called by the GC if
the object is not used anymore. The finalizer will also be called from
within a different thread...

IDisposable.Dispose is just a normal method!

In general you can say:
Implement IDisposable if you need to free unmanaged resources.

And additionally implement a finalizer if you want to free the unmanaged
resources if the user of your class forgot to call "Dispose".


Mostly all .NET classes which implements IDisposable, also implements an
finalizer.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

you should read the article 'Implementing Finalize and Dispose to Clean Up
Unmanaged Resources' in the msdn
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpgenref/html/cpconfinalizedispose.htm)
 
D

David Levine

A finalizer will always be called by the CLR when it is unreachable and the
GC runs a collection cycle; a dispose method is just another method that
must be invoked by user code. That being said, there are certain best
practices that have evolved that impact this. A finalizer is guaranteed
(under normal conditions) to get called (though you don't know when); a
Dispose is never guaranteed to get called, but is entirely under
programmatic control, so it can be deterministic.

If a class implements a finalizer it should also implement a Dispose. I
usually also do the converse unless there are good reasons for not doing so;
i.e. if it implements a Dispose I also implement a finalizer unless there
are thousands of these objects and they would swamp the finalization queue.

If a class implements the IDisposable interface and a user of that class
does not call its Dispose method I tend to treat that as a programming
error. The Dispose method should suppress finalization of the object. If the
finalizer actually does get called I output a trace message indicating that
the finalizer was called and I start a bug hunt to determine why the Dispose
was not called.

In terms of requirements, if a class encapulates an unmanaged resource,
then it should implement a finalizer to ensure that the resource is properly
cleaned up. It should also implement a Dispose to make the cleanup more
deterministic so that when the cleanup occurs is then under control of the
application instead of when the GC runs (i.e. random).

If a class owns other managed objects that implement Dispose then it should
also implement Dispose so it can invoke Dispose on its owned objects.

These are not universal, and some are controversial, but I find this to be a
reasonable set of "best practices".
 
C

Chee Pin Cheam

Jochen said:
Hi Klaus Aschenbrenner,




Finalizer and IDisposable are primarily two different things...

If a class implements a finalizer, it will (mostly) be called by the GC if
the object is not used anymore. The finalizer will also be called from
within a different thread...

IDisposable.Dispose is just a normal method!

In general you can say:
Implement IDisposable if you need to free unmanaged resources.

And additionally implement a finalizer if you want to free the unmanaged
resources if the user of your class forgot to call "Dispose".


Mostly all .NET classes which implements IDisposable, also implements an
finalizer.
One normally implement IDisposable interface when deterministic object
destruction is needed, especially if the object in matter are holding on
to some critical resources. With IDisposable interface, the caller of
the object can call Dispose method of your object so that you can do the
neccessary clean-up.

Chee Pin
 
J

Jochen Kalmbach

Hi Chee Pin Cheam,
One normally implement IDisposable interface when deterministic object
destruction is needed, especially if the object in matter are holding
on to some critical resources. With IDisposable interface, the caller
of the object can call Dispose method of your object so that you can
do the neccessary clean-up.

IDisposable does *not* guarantee "deterministic object destruction"!!!

It is only an hint to the user of the class that he/she *should* call
"Dispose" if the object is not longer used.


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
C

Chee Pin Cheam

Jochen said:
Hi Chee Pin Cheam,




IDisposable does *not* guarantee "deterministic object destruction"!!!

It is only an hint to the user of the class that he/she *should* call
"Dispose" if the object is not longer used.
My apologies. I read Dispose guidelines and realize the mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top