[racket] FFI, string, malloc/free

From: Hendrik Boom (hendrik at topoi.pooq.com)
Date: Sat Jun 15 08:26:39 EDT 2013

On Sat, Jun 15, 2013 at 02:15:04PM +0200, Laurent wrote:
> On Sat, Jun 15, 2013 at 1:55 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> 
> > What about
> >
> > (define-lib foo (fun _string -> _void))
> >
> > (foo "Test")
> >
> > ?
> >
> > Will be the pointer from string "Test" freed?
> >
> 
> I *think* Racket will garbage collect the "Test" value when it is not used
> anymore on the Racket side, but it won't know what happens on the ffi C
> side (hence the "ffi/unsafe").
> So I think the lib may point to a freed value if you're not cautious.
> 
> 
> > And how to make it retain, if I need to save it on the foreign side?
> >
> 
> You can probably at least simply store it in a global variable. There may
> be a better way to do it though.
> For example (not tested) :
> #lang racket
> (define dontfreethem '())
> (define (dontfreeme x)
>   (set! dontfreethem (cons x dontfreethem))
>   x)
> 
> (foo (dontfreeme "Test"))
> 
> Laurent

But the garbage collector can still move the string when collecting, so 
protecting it from being freed isn't enough.

Isn't there some way to declare an object immovable?

-- hendrik


> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.