To clarify a little - while the arguments to a
'foreign import' should at least live for the
duration of an invocation of a (safe) foreign
import, there's no guarantee that heap references
you pass out won't move. Hence, the scheme outlined
in the code of the original message isn't really going
to work with or without this extra constraint. [This is
one good reason why I'm considering restricting the use
of (Mutable)ByteArrays to unsafe foreign imports only.]

So, why bother with this extra constraint on argument
lifetimes, if it won't be of any use here? Well, it is
very useful when working with ForeignObjs.

--sigbjorn

Sigbjorn Finne (Intl Vendor) <[EMAIL PROTECTED]> writes: 
> 
> It is currently not, but I think it should be. Will update the FFI
> spec to mention this issue.
> 
> --sigbjorn
> 
> > -----Original Message-----
> > From: Sven Panne [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, September 22, 1999 15:41
> > To: GHC Users
> > Subject: Staying alive
> > 
> > 
> > In a recent discussion with Manuel Chakravarty the 
> following question
> > arose. Given the following code:
> > 
> >    foo = do
> >       ba <- newMutableByteArray ...
> >       ...
> >       bar ba
> >       -- ba not used here anymore
> > 
> >   foreign import ... bar :: MutableByteArray ... -> IO ()
> > 
> > Let's further assume that bar makes callbacks to 
> Haskell-land where a
> > GC occurs. Now the subtle question: Is it *guaranteed* that ba is
> > considered alive until bar returns or not? Either way, this 
> should be
> > clarified in the FFI documentation.
> > 
> > Background: A lot of APIs (e.g. GTK+ and OpenGL :-) have functions
> > expecting a pointer to some data. One possible way to use them from
> > Haskell is
> > 
> >    * malloc area
> >    * fill area with data
> >    * call API function with pointer to area
> >    * explicitly free malloced area
> > 
> > Using (Mutable)ByteArrays would simplify thing here, especially the
> > last step would not be needed.
> > 
> > Cheers,
> >    Sven
> > -- 
> > Sven Panne                                        Tel.: 
> > +49/89/2178-2235
> > LMU, Institut fuer Informatik                     FAX : 
> > +49/89/2178-2211
> > LFE Programmier- und Modellierungssprachen              
> > Oettingenstr. 67
> > mailto:[EMAIL PROTECTED]            
> > D-80538 Muenchen
> > http://www.informatik.uni-muenchen.de/~Sven.Panne
> > 
> 

Reply via email to