FFI Bug - Finalizer always called with NULL pointer?

Brian Hulley brianh at metamilk.com
Sat Apr 8 03:25:46 EDT 2006

Hi -
I've discovered that the finalizer in the code below is always called with a 
NULL pointer even though the pointer returned by the allocation routine is 
always not NULL.
Is this a known bug, and if so, are there any workarounds?
I'm using ghc version 6.4:

data RawFont
newtype Font = Font (ForeignPtr RawFont)

foreign import ccall duma_createFont ::
    CString -> Int -> Bool -> Bool -> Bool -> IO (Ptr RawFont)
foreign import ccall duma_releaseFont ::
    FunPtr (Ptr RawFont -> IO ())

createFont :: String -> Int -> Bool -> Bool -> Bool -> IO Font
createFont name pointSize bold italic underline = block $ do
   f <- (withCString name $ \cname ->
           duma_createFont cname pointSize
                bold italic underline) >>=
        newForeignPtr duma_releaseFont -- Gets a NULL pointer!!!
   return $ Font f

Thanks, Brian. 

More information about the Glasgow-haskell-users mailing list