Finalizers and FFI

Gracjan Polak gracjan at
Thu Jun 10 11:33:47 EDT 2004

Alastair Reid wrote:
>>>import Foreign.ForeignPtr
>>I couldn't get finalisers to work either with the newForeignPtr from
>>this module. I didn't know how to create a proper FunPtr.
> You create a FunPtr using foreign import:
>   foreign import ccall "malloc.h &free" free_ptr :: FunPtr (Ptr a -> IO ())

foreign import stdcall "windows.h &UnmapViewOfFile"
               funptrUnmapViewOfFile :: FunPtr (Ptr a -> IO ())

Basically I'd love to do (in Windows world):

mapTheFileToMemory = do
    handle  <- winOpenFile(...)
    mapping <- winCreateFileMapping(...)
    view    <- winMapViewOfFile(...)
    finview <- newForeignPtr funptrUnmapViewOfFile view
    return finview

Strangely enough my finalizer run always this time, no need to say 
performGC or yield'ing at the end of main. But it crashes my program :(

Warnings in compilation are also strange:

$ ghc -package win32  --make interlvIO.hs -o interlvIO.exe
Chasing modules from: interlvIO.hs
Compiling Main             ( interlvIO.hs, interlvIO.o )
Linking ...
Warning: resolving _UnmapViewOfFile by linking to _UnmapViewOfFile at 4
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups

I did not find any of those flags. Searching sources downloaded from 
website today also does not say anything about stdcall fixups. Changing 
calling convention from stdcall to ccall in import clause did not help 

At the end of (correct) run my program dies with:

interlvIO.exe: internal error: resumeThread: thread not found
     Please report this as a bug to glasgow-haskell-bugs at,

So basically I have no idea how to make finalizer out of UnmapViewOfFile :(

Any ideas where to go now?


More information about the Glasgow-haskell-users mailing list