addForeignPtrFinalizer
Ross Paterson
ross at soi.city.ac.uk
Wed Sep 25 04:33:04 EDT 2002
On Wed, Sep 25, 2002 at 06:18:35AM +0100, Alastair Reid wrote:
> > Consider regcomp in Test.Regex.Posix.
>
> I didn't find this example at all convincing because the existing
> finalizer was written as a monolithic whole and this is a simpler,
> clearer way to write the finalizer.
If the libraries used newForeignPtr with the type now given in the FFI
spec, that finalizer would have to be rewritten in C:
void finalize_reg(regex_t *r) { regfree(r); hs_free(r); }
Since the order of multiple finalizers is unspecified, one is forced
to define a single finalizer in the foreign language that calls your
finalizers in the right order. If the struct were allocated with
mallocForeignPtr, you need another C call to use as the finalizer for
that, say hs_f_free(). Each foreign language needs a name for each
finalizer.
It would work, but it's ugly, and it underlines the uselessness of
addForeignPtrFinalizer without a specified order.
More information about the FFI
mailing list