[Haskell] using the ffi with callbacks

Taral taralx at gmail.com
Thu Jul 20 15:41:01 EDT 2006


On 7/20/06, Evan Martin <martine at danga.com> wrote:
> The tricky part is that to pass in Haskell functions, I need to use
> the FFI "wrapper" import, which means I need to later free them.  But
> the only place I can free them is within the "free" callback, and I've
> just discovered this isn't allowed!

free_fcn is normally a static function. It shouldn't need to free
itself. I'm thinking something like:

type Callback = StablePtr (Callback, DataStructure)-> IO ()
foreign import ccall "wrapper" mkCallback :: Callback -> IO (FunPtr Callback)
foreign export "free_fcn" free_fcn :: Callback

free_fcn sp = do
    (cb, ds) <- deRefStablePtr sp
    freeStablePtr sp
    freeHaskellFunPtr cb

-- 
Taral <taralx at gmail.com>
"You can't prove anything."
    -- Gödel's Incompetence Theorem


More information about the Haskell mailing list