[Gc] Re: Do finalizers run at exit?

Bruce Hoult bruce at hoult.org
Thu Dec 27 01:45:20 PST 2007

On Dec 27, 2007 9:39 PM, Achilleas Margaritis <axilmar at otenet.gr> wrote:
> And another question: why finalization is good after a collection, but
> not at program exit? what's the difference? both situations seem the
> same to me.

They're COMPLETELY different.

Finalization after a collection happens only to objects that are
unreachable, which means that nothing else depends on them.

If you want to run finalizers for all objects at program exit then you
are running them on objects which ARE STILL reachable and which may
depend on each other. This will surely violate invariants unless you
have a very very simple object graph.

If you run your own cleanup list you can at least guarantee that
objects are cleaned up in the opposite order from which they were
created.  The order in which finalizers are run, if forced to, will be
essentially random.

More information about the Gc mailing list