newForeignPtr

Ross Paterson ross at soi.city.ac.uk
Mon Feb 3 05:47:59 EST 2003


On Mon, Feb 03, 2003 at 09:20:03PM +1100, m v wrote:
> 
> I do not understand the rational of the type of:
> 
> 	newForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO (ForeignPtr 
> 	a)
> 
> suppose I have a c function
> 
> 	foreign import ccall fooalloc :: Ptr Foo
> 
> and another
> 
> 	foreign import ccall foofree :: Ptr Foo -> IO ()
> 
> now I want to combine fooalloc and foofree to create a Foreign pointer 
> without wrapping foofree again, as in:
> 
> 	 foreign import "wrapper" mkfoofree :: FunPtr (Ptr Foo -> IO ())
> 
> which I think counter productive - why should I import a function only to 
> have to re-export it again !

You don't need to wrap it -- you can import its address with

	foreign import ccall "&foofree" foofreeptr :: FunPtr (Ptr a -> IO ())

> previouse proposals did not require this - or am I missing something ?
> 
> why should the finaliser be a foreign function ?

It's a long story, but it turned out that Haskell side finalizers are hard
to implement correctly in conventional single-threaded implementations.
After a long discussion on this list in October last year, SimonM came up
with a killer example:

	http://www.haskell.org/pipermail/ffi/2002-October/000866.html
	http://www.haskell.org/pipermail/ffi/2002-October/000910.html

For a summary of the discussion, see

http://cvs.haskell.org/cgi-bin/cvsweb.cgi/haskell-report/ffi/finalizers.txt

However, only Hugs implements this part of the spec at the moment.



More information about the FFI mailing list