Proposed change to ForeignPtr

Manuel M T Chakravarty chak at
Tue Sep 10 03:28:52 EDT 2002

Manuel M T Chakravarty <chak at> wrote,

> We seem to have a consensus on this one.  We change the type
> of the existing functions to
>   newForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO (ForeignPtr a)
>   addForeignPtrFinalizer :: ForeignPtr a -> FunPtr (Ptr a -> IO ()) -> IO ()
> For GHC, I propose to put the closure-based versions into an
> extra module (that's easy enough with the hierarchical
> libraries).  This makes changing over old code easier, as it
> merely requires to alter the import and not all occurences
> of the functions.
> Any objections?

I have changed this in the spec now.  I attach the wording
used in the spec.



\item[newForeignPtr ::\ Ptr a -> FunPtr (Ptr a -> IO ()) -> IO (ForeignPtr a)]
  Turn a plain memory reference into a foreign object by associating a
  finalizer with the reference.  The finalizer is represented by a pointer to
  an external function, which will be executed after the last reference to the
  foreign object is dropped.  On invocation, the finalizer receives a pointer
  to the associated foreign object as an argument.  Note that there is no
  guarantee on how soon the finalizer is executed after the last reference was
  dropped; this depends on the details of the Haskell storage manager. The
  only guarantee is that the finalizer runs before the program terminates.

  Whether a finaliser may call back into the Haskell system is system
  dependent.  Portable code may not rely on such call backs.
\item[addForeignPtrFinalizer ::\ ForeignPtr a -> FunPtr (Ptr a -> IO ()) -> IO
  ()] Add another finalizer to the given foreign object. No guarantees are
  made on the order in which multiple finalizers for a single object are run.

More information about the FFI mailing list