[Haskell-cafe] ForeignPtrs with liveness dependencies

Einar Karttunen ekarttun at cs.helsinki.fi
Tue Jul 26 09:14:50 EDT 2005

"Simon Marlow" <simonmar at microsoft.com> writes:
>> Now the association becomes
>> associate (Foo _ ref) bar =
>>   atomicModifyIORef ref (\lst -> (touchForeignPtr bar : lst, ()))
> Isn't that equivalent to using addForeignPtrFinalizer?  I don't think
> this fixes anything: the finalizer for bar can still run before the
> finalizer for foo.

foo has a single finalizer which is defined like:

fooFinalizer cfoo ref = do cdeleteFoo cfoo
                           vs <- readIORef ref
                           mapM_ (\c -> c) vs

and foo is created like

createFoo ptr = do ref <- newIORef []
                   fp <- newForeigPtr ptr (fooFinalizer ptr ref)
                   return (Foo fp ref)

As the finalizer of foo references the IORef which contains
the list of actions containing the "touchForeignPtr bar"
the finalizer of foo is run first. The finalizer to bar
should be able to run only when the touchForeignPtr has been 
executed in the mapM_ which only happens after foo 
has been cleaned up - if I understand things correctly.

- Einar Karttunen

More information about the Haskell-Cafe mailing list