[Mono-list] Finalizer never called

Giuseppe Greco giuseppe.greco@agamura.com
Fri, 01 Aug 2003 14:13:32 +0200


On Fri, 2003-08-01 at 13:58, Jonathan Pryor wrote:
> <snip/>
> > A question: could really happen that a finalizer is never called?
> > If so, how would managed resources be released?
> 
> Is it possible for finalizers to never be called?  Yes.  Why?  Because
> of this:
> 
> 	class BadClass {
> 		~BadClass () {
> 			System.GC.ReRegisterForFinalize (this);
> 		}
> 	}
> 
> This class will never finalize, because every time the GC attempts to
> finalize the class, it requests that it be kept alive.
> 
> The basic idea is that the GC keeps track of how many objects need to be
> finalized.  During process shutdown, as long as this number is
> declining, finalizers will continue to be run.  If this number stops
> declining before reaching 0 (which would occur with the class above),
> finalizers stop being executed, just so that the process can be shut
> down within a reasonable timeframe.
> 
> Additionally, finalizers are run on a different thread, so that if a
> finalizer never terminates (e.g. "while (true) {}"), the finalizer
> thread can be terminated, allowing the process to shut down in an
> orderly fashion.
> 
> It's unlikely that either of these situations will occur, but they need
> to be taken into consideration.  Most of the time, finalizers should
> always be run.  I would consider failure to do so to be a bug in the
> runtime.

Yes. That's exactly what I wanted to hear... In well designed classes
finalizers should always be run.

Thanks,
Gius_.

> 
>  - Jon
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
-- 
----------------------------------------
Giuseppe Greco

::agamura::

phone:  +41 (0)91 604 67 65
mobile: +41 (0)76 390 60 32
email:  giuseppe.greco@agamura.com
web:    www.agamura.com
----------------------------------------