Finalizer problems..

Simon Marlow simonmar@microsoft.com
Wed, 17 Jan 2001 03:08:48 -0800


> > I would like to run some code when something is
> > garbage-collected. Fine, he says, just the job for
> > finalizers (module Weak). Unfortunately, they don't
> > seem to offer a general solution, as the code below
> > demonstrates (presumably, smallish things are copied,
> > not shared, so that the finalizers run too early?).
> 
> Yes, small Ints (approximately 30 bits worth) and Chars have a special
> representation which makes it impossible to tell if they have 
> been GC'd
> 
> GHC has a similar problem for Chars and small ints (-15..15 I think).

GHC has similar problems for just about all types, due to agressive
inlining and other optimisations.  We've found that the only reliable
types on which you can set a finalizer are the primitive heap-resident
ones, like ForeignObj# and MutVar#.  That's why using
addForeignFinalizer is better than using the generic addFinalizer.

We dreamed up various hacks to subvert the optimisations, but haven't
found any clean solutions yet.  The problem is one of "identity": how to
give an object a name such that you can refer to that particular object.

Cheers,
	Simon