Bug in touchForeignPtr?

Benjamin Franksen benjamin.franksen at bessy.de
Sat Nov 20 18:02:00 EST 2004


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.

Top level things with identity are evil.	-- Lennart Augustsson
-------------- next part --------------
import Foreign hiding (newForeignPtr)
import Foreign.Concurrent
import System.Mem (performGC)

type A = ForeignPtr Int

type B = ForeignPtr Bool

newA :: IO A
newA = do
  (pa::Ptr Int) <- malloc
  newForeignPtr pa $ do
    putStrLn "before finalizing A"
    free pa
    putStrLn "after finalizing A"
newB :: A -> IO B
newB fpa =
  withForeignPtr fpa $ \pa -> do
    (pb::Ptr Bool) <- malloc
    newForeignPtr pb $ do
      putStrLn "before finalizing B"
      free pb
      putStrLn "after finalizing B"
      touchForeignPtr fpa

main = do
  a <- newA
  b <- newB a

More information about the Glasgow-haskell-users mailing list