exercising the storage manager

Simon Marlow simonmar at microsoft.com
Tue Jun 10 04:55:07 EDT 2003


 
> g'day everyone,
> 
> I'm getting segv's from a program using the FFI (ghc5.0x) on 
> MacOS X only,
> i.e. not on Linux, my primary development platform.
> 
> So... does anyone have any tips for showing up dodgy uses of 
> the FFI wrt
> memory handling? What settings for GHC's garbage collector 
> will create the
> most pathological situations?
> 
> Also, I have some qualms about foreignPtrToPtr; by it's type, 
> it seems to
> be exactly what you want most of the time, but due to the "if 
> this is the
> last reference, the finalizers may run" caveat it almost always isn't.
> I note that it already has some warnings attached to it, but...

Most of the time you should be using 'withForeignPtr'.  I don't think
there can be any legitimate uses for foreignPtrToPtr unless there is a
touchForeignPtr that is guaranteed to happen later.  And that's a tricky
guarantee, because it involves reasoning about strictness.

Does anyone else think that foreignPtrToPtr should really be called
unsafeForeignPtrToPtr?

> - does anyone have a safe use for it that doesn't involve 
> touchForeignPtr?
> 
> - can the FFI spec somehow group together all functions that 
>   may introduce memory leaks or unsafety?

Unsafety is normally indicated with an 'unsafe' prefix on the function
name.  Memory leaks are a different matter - since malloc on its own can
introduce a memory leak, I'm not sure how you'd indicate that.

Cheers,
	Simon




More information about the FFI mailing list