Finalizers 2: Bayou Justice (and Weak pointers to boot...)

Malcolm Wallace Malcolm.Wallace at
Mon Oct 14 06:07:27 EDT 2002

John Meacham <john at> writes:

> Just musing about the need some people have for ForeignPtrs to be
> deallocated deterministly, perhaps something like C#'s 'using' construct
> is in order which would enforce a stacklike allocation policy for
> ForeignPtrs... 

If you need deterministic finalisation, we already have IO.bracket:

    bracket :: IO a -> (a->IO b) -> (a->IO c) -> IO c

The first argument generates the value 'a', the second finalises it
even in the presence of exceptions, and the third argument is the
action that uses the value.  Just instantiate the type variable 'a'
to Ptr rather than ForeignPtr.

> and if we are adding things to the FFI spec, how about the empty data
> declaration? it is painfully simple to implement and would make FFI code
> a lot cleaner as it tends to depend heavily on placeholder types..

Hugs and ghc already have it (AFAIK).  I recently added it to nhc98.
Do you think it should become a formal part of the FFI standard?  Or
is it best left as another "common" extension of Haskell'98 that just
happens to be implemented everywhere?


More information about the FFI mailing list