FFI Ptr question

Derek Elkins ddarius@hotpop.com
Thu, 3 Jul 2003 01:40:46 -0400


On Thu, 3 Jul 2003 11:34:08 +0800
paul@theV.net wrote:

> If a C function returns a (void*), let's say:
> 
> data MyObj = MyObj
> 
> foreign import ccall "static myobj_ffi.h new_obj" newMyObject :: IO
> (Ptr MyObj)
> 
> ptr <- newMyObject
> 
> should I free the ptr afterwards or not?

Assuming that it should be freed even in C and nothing else is managing
it then I believe the answer is yes.  You can use a finalizer
to do it.

> 
> If I have another imported C function:
> 
> foreign import ccall "static myobj_ffi.h print_obj" printMyObject ::
> Ptr MyObj -> IO ()
> 
> Can I safely assume the C function print_obj(void *) always get the
> same pointer even if I call (printMyObject ptr) multiple times?

What other pointer would it pass?