[Haskell-cafe] Finalizers on FunPtrs

Maurí­cio CA mauricio.antunes at gmail.com
Wed Aug 12 10:11:00 EDT 2009

I see in Foreign.Ptr documentation that it is good practice to
call freeHaskellFunPtr when a pointer to a Haskell function
(obtained using 'dynamic' stubs) is not going to be used anymore.

Is it possible to do that in a way that is similar to ForeignPtr
behavior, where pointers are finalized when GC knows they're not
going to be used anymore? See this attempt:

In an IO monad:
   a <- giveMeFunPtr
   b <- castFunPtrToPtr a :: IO (Ptr something)
   foreignFunPtr <- newForeignPtr funPtrFinalizer b :: IO (ForeignPtr something)

With funPtrFinalizer a FunPtr to:
   baseFunPtrFinalizer p = freeHaskellFunPtr . castPtrToFunPtr p

Is something like that expected to work, i.e., would I get a
ForeignPtr that I could cast and give to foreign functions, and
still expect that my function pointer would be freed when not
needed anymore?


More information about the Haskell-Cafe mailing list