Re: More Finaliser Trouble

Wolfgang Thaller <[email protected]>
Newsgroups gmane.comp.lang.haskell.ffi
Message-ID <[email protected]>
Ashley Yakeley <[email protected]> wrote:

> In article <[email protected]>,
> Fergus Henderson <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.