Bug in touchForeignPtr?

Simon Marlow simonmar at microsoft.com
Mon Nov 22 08:45:01 EST 2004

On 20 November 2004 23:02, Benjamin Franksen wrote:

> I am using Foreign.Concurrent.newForeignPtr and touchForeignPtr
> inside the finalizers to express liveness dependencies as hinted to
> by the documentation. This doesn't seem to work, though, or at least
> I can't see what I've done wrong. I attached a test module; compiled
> with ghc -fglasgow-exts --make TestForeignTouchBug.hs, ghc version
> 6.2.2, this gives 
> .../foreigntouchbug > ./a.out
> <hit enter here>
> before finalizing A
> after finalizing A
> before finalizing B
> after finalizing B
> <hit enter here>
> I expected the order of the finalizer calls be be the other way
> around, since the finalizer for the Bs explicitly touches the A value.

The problem is that the runtime is running all outstanding finalizers at
the end of execution, without regard for GC dependencies like the ones
introduced by touchForeignPtr.

I've been planning to remove this automatic running of finalizers for
other reasons.   However, then you will get absolutely no guarantee that
your finalizer will ever run at all (indeed, the property isn't always
true right now, but it is usually true).

Let me share with you something that I've come to appreciate over the
last few years:

  Finalizers are almost always not the right thing.

Finalizers look terribly attractive, but often lead to a huge can of
worms - best avoided if at all possible.


More information about the Glasgow-haskell-users mailing list