> The function is described as unsafe because the result loses the
> finalizer, right?
> 
> So it is in fact unsafe to store a list of such Addrs in malloced
> memory, passing the pointer to the array to a C function, because
> they can be garbage collected and finalized during storing them,
> before the C function is invoked.
> 
> (Fortunately in my case it does not harm to invoke the finalizer from
> the garbage collector called from Haskell code called from inside
> the C function.)
> 
> But I really need to pass them to a C function. Is there a simple
> way of preventing them from being finalized too early?
> 
> Hmm, maybe something like:
>     foldr seq theListOfForeignObjs (return ()) :: IO ()
> after calling the C function. Any more elegant idea?

The Official Way to do this is to make a StablePtr for each ForeignObj.
This stops the ForeinObj from becoming garbage (and hence being finalized)
until you later call freeStablePtr.

Actually, you might as well put all the ForeignObjs in a list, and make one
StablePtr for the list.  Cunning, eh?

Cheers,
        Simon

Reply via email to