More Finaliser Trouble

Wolfgang Thaller wolfgang.thaller at gmx.net
Tue Jul 15 05:53:09 EDT 2003


Ashley Yakeley <ashley at semantic.org> wrote:

> In article <20030714071832.GA3813 at ceres.cs.mu.oz.au>,
> Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> 
>>> Should there perhaps be something like ...
>>>> newForeignPtrWithUserData :: Ptr a -> Ptr b -> FunPtr (Ptr a -> Ptr b -> IO > ()) -> IO (ForeignPtr a)
>>>> ... where the second argument is a "userData" pointer that gets passed on > to the finalizer?
>> 
>> IMHO yes!
> 
> Well now what if you want to pass two extra pointers? It's true you could "glue" them together [...]

Of course. That's what I have to do with all userData pointers I use in C,
so if I use C finalizers, I'll put up with that...

> [...] but equally, you can glue together the two pointers passed to newForeignPtrWithUserData.
> 
> Bear in mind that your newForeignPtrWithUserData, "Ptr a" and "Ptr b" are effectively interchangeable. You have one object, a ForeignPtr, that holds two pointers. When the ForeignPtr gets finalised, it passes both pointers to its finaliser function (assuming it has exactly one). There really isn't a lot of difference between the two of them when there's only one finaliser.

I wouldn't say that there's no difference: The type of the ForeignPtr depends on
the type of the first pointer only. You can get the first pointer from the ForeignPtr
without having to know anything about the finalizer.

When you create pointers using different methods (e.g. standard C malloc,
pointers to static storage, a Java VM's storage manager, etc.), you will need different finalizers,
and some finalizers require extra data to operate. However the resulting ForeignPtrs
should all have the same type, as they are pointers to the same kind of data.

Cheers,

Wolfgang




More information about the FFI mailing list