Finalizers and FFI

Arjan van IJzendoorn afie at
Wed Jun 9 14:40:00 EDT 2004

HI Gracjan,

> I would like to attach finalizer (written in Haskell) to some pointer. 
> When the pointer won't be needed any more, finalizer should run. So 
> here is the code:

> 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. In 
Foreign.Concurrent there is a newForeignPtr that is easier to use:

newForeignPtr :: Ptr a -> IO () -> IO (ForeignPtr a)

This one worked beautifully for me. In your code something like:

import Foreign.ForeignPtr hiding (newForeignPtr)
import Foreign.Concurrent

    ... fptr <- newForeignPtr ptr (finDoIt ptr)

Hope this helps,

More information about the Glasgow-haskell-users mailing list