addForeignPtrFinalizer

Simon Marlow simonmar at microsoft.com
Wed Sep 25 05:25:31 EDT 2002


> On Wed, Sep 25, 2002 at 09:33:04AM +0100, Ross Paterson wrote:
> > If the struct were allocated with mallocForeignPtr, you need another
> > C call to use as the finalizer for that, say hs_f_free().
> 
> Sorry, that's wrong: If the struct were allocated with 
> mallocForeignPtr,
> you're stuck.  You've no choice but to use addForeignPtrFinalizer,
> except that you can't because the order of finalizers is unspecified.

I can guarantee in GHC that if you add a finalizer to a ForeignPtr made
with mallocForeignPtr, then it will run before the memory is released.

However, to provide a general ordering property on finalizers would be
quite awkward in GHC - I just can't think of an easy way to do it.  One
way is to add a unique tag to each weak pointer object and sort them
before running the finalizers, but also the finalizers have to run
sequentially somehow (currently there's a hack to run some finalizers in
the same thread, but it's nothing more than a performance hack; I don't
think I could use it to provide reliable ordering).

I tell you what, I'll implement finalizer ordering if you guys implement
full Haskell finalizers :-)

Cheers,
	Simon



More information about the FFI mailing list