FFI Bug - Finalizer always called with NULL pointer?

Brian Hulley brianh at metamilk.com
Sat Apr 8 04:13:15 EDT 2006

Hi -

When I use -O2 to compile, gcc gives a compilation error: too few arguments 
to function "duma_releaseFont"
When I look at the ghc output with -ddump-cmm, this seems to be where the 
bug is:

r2MP_entry() {
    // SNIP - all kinds of stuff here
 call "ccall" suspendThread((BaseReg, NoHint))[(_c36U, NoHint)];
 call "ccall" duma_releaseFont()[(_c36V, PtrHint)]; -- @@@@ MISSING ARG
 call "ccall" resumeThread((_c36U, NoHint));
    // SNIP more stuff
 jump stg_gc_enter_1;

So ghc is not using supplying an argument when it should be supplying 
duma_releaseFont with the raw pointer contained inside the ForeignPtr being 
garbage collected.
This also explains why the program crashes even with native compilation 
(debug build).

So the bug is not that it is supplying a NULL arg but that it is not 
supplying any arg in the first place.

I've also tried using addForeignPtrFinalizer instead but this doesn't help: 
GHC is determined to not give the finalizer any argument.

createFont name pointSize bold italic underline = block $ do
   fRaw <- (withCString name $ \cname ->
              duma_createFont cname pointSize
                          bold italic underline)
   f <- newForeignPtr_ fRaw
   addForeignPtrFinalizer duma_releaseFont f -- no luck either
   return $ Font f

Regards, Brian. 

More information about the Glasgow-haskell-users mailing list