[gclist] Destructor FAQ

Mario Wolczko Mario.Wolczko@Eng.Sun.COM
Thu, 7 Mar 1996 11:25:06 -0800


Nobody has mentioned post-mortem finalization in any detail, so I'll
throw in a paragraph or two...

David Gadbois's contribution on destructors mentioned possible problems
when GC systems also incorporate finalization.  Post-mortem
finalization attempts to solve these problems.

I believe it was invented by Frank Jackson and Dave Ungar for
ParcPlace Smalltalk (I'm not absolutely certain of this, but I can
check with Dave Ungar next week).  In PPS is it used in conjunction
with weak pointers.  All weak pointers are contained within instances
of a special class, WeakArray.  When an object is reclaimed and has
weak references, the garbage collector nils out the weak references.
This solves one problem: because the object really is reclaimed, there
is no chance that finalization code can revive it.  When it's gone,
it's gone.  However, we still need some way to actually perform
finalization.

In addition to nilling out weak pointers, the collector makes
available to the mutator the identity of each weak array containing
references that it nilled out, and the indexes into that array of such
references.  It is then up to the mutator to figure out from this
information what finalization actions are required.  Typically, the
mutator maintains for each weak array another array of non-weak
pointers to copies of the objects referenced from the weak array.  The
copies contain the essential state required for finalization (such as
file descriptors and window handles that need to be closed).  Once the
mutator has been informed by the collector which elements of the weak
array have been nilled, it can then use the information in the copy to
perform finalization actions, and then nil out the strong pointer to
the copy.  The copy is typically only referenced from this array, so
that causes it to become garbage and be reclaimed.

So, the key ideas of this scheme are that objects referenced only by
weak pointers are reclaimed anyway, the weak pointers are nilled by
the collector, and the mutator can find out which weak pointers have
just been nilled, in order to perform finalization.

The only published description of this technique that I'm aware of is
in the ParcPlace Smalltalk manuals.

-Mario