mallocForeignPtr vs. C

Axel Simon axel.simon at
Tue Jul 13 16:28:29 EDT 2010

On Jul 13, 2010, at 22:17, Edward Z. Yang wrote:

> Excerpts from Axel Simon's message of Tue Jul 13 16:03:01 -0400 2010:
>> If your C code has a way to properly unref a pointer then you could
>> wrap your ForeignPtr in a StablePtr and pass that to C land. Once C
>> has freed the StablePtr the ForeignPtr can become dead when Haskell
>> has dropped all references and it will be garbage collected.
> Does ForeignPtr have a guaranteed representation from C land?   I feel
> like you want to create a stable pointer and then unsafely cast the
> foreign pointer into a real pointer, which would effectively fizzle
> the original foreign pointer's finalizer.

Well, if the C code hangs on to the StablePtr that wraps the  
ForeignPtr, its finalizer won't be run. But can run again once the  
StablePtr is freed. So you can take out the Ptr in the ForeignPtr and  
use it in C land as long as C holds on to the StablePtr.


More information about the Glasgow-haskell-users mailing list