garbage collection and Ptr/ForeignPtr/StablePtr

Simon Marlow simonmar at microsoft.com
Mon Sep 22 15:11:04 EDT 2003


 
> Should I expect that Ptr memory allocated with malloc is 
> garbage collected
> when no longer used by the Haskell runtime? The FFI spec 
> doesn't say so
> explicitly (that I could see); AFAICT this is how the pointer 
> types behave:
>   Ptr : freed when no longer used/referenced
>   ForeignPtr : like Ptr, but will also run a finaliser when freed
>   StablePtr : not freed or collected

No, all memory that you allocate with the FFI must be freed explicitly.
You can arrange for memory to be deallocated when no longer referenced
by using a ForeignPtr, and using the finalizer to free the memory.  Bear
in mind that if your application has large, or strict, memory
requirements, then relying on the ForeignPtr method may not be the best
solution, because finalizers  can be unpredictable.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list