"Simon Peyton-Jones" <[EMAIL PROTECTED]> writes:

> |   - in GHC, references from a finalizer are treated as weak
> |     references, that is, they don't keep anything alive.
> 
> There's a good reason for this: otherwise the finaliser would keep alive
> the very object whose death it is watching for.

This is probably true for weak pointers, but in the design of the
Haskell FFI, it is most definitely not the case.

In the FFI, the original object is indeed referenced within the
finaliser, but the ForeignPtr is not.  In fact, ideally the original
object should be non-live in the main program from the very moment
the finaliser is attached to it.

Recall the signature of the operation:

  newForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO (ForeignPtr a)

Once the ForeignPtr is created, the original Ptr can and must be
discarded in the main program.  The Ptr itself is kept live after that
only by the finaliser: for the purposes of the rest of the program,
the object has been transmuted into a composite ForeignPtr object
that acts for all the world just like the original Ptr except that
it has a different identity.

I think this encapsulation and separation of types/values is an essential
feature of finalisation in the FFI.

Regards,
    Malcolm
_______________________________________________
Cvs-ghc mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to