[Gc] Re: Weak hash tables and cycles

Ludovic Courtès ludo at gnu.org
Fri Nov 7 06:32:54 PST 2008

Hi Hans,

Hans Boehm <Hans.Boehm at hp.com> writes:

> If you leave the disappearing link to K, but then hide the link to V from
> the collector and register a finalizer for V, V's finalizer will be
> invoked ater a collection during
> which V is only reachable from the table.  V's finalizer can check whether
> the disappearing link to K is still there.  If it is, it simply
> reregisters itself as a finalizer for V.  If the disappearing link is
> gone, it doesn't reregister itself, allowing V to be collected in the
> next cycle.

Hmm, good idea.

(Another way to look at it is that we're always creating doubly-weak
hash tables, but when we really want a weak-key hash table, a finalizer
for V is created to keep it alive.)

However, this technique requires a way to multiplex per-object
finalizers, so that the finalizer registered for V by the hash table
implementation doesn't override a previous user-specified finalizer.

It looks like the need to multiplex per-object finalizers arises quite
often.  It was also highlighted in
https://osdir.com/ml/programming.garbage-collection.boehmgc/2003-03/msg00013.html .
Maybe libgc could provide a facility for this?

Thanks for your help,

More information about the Gc mailing list