[racket] FFI, string, malloc/free

From: Laurent (laurent.orseau at gmail.com)
Date: Sat Jun 15 08:15:04 EDT 2013

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))

(foo (dontfreeme "Test"))

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130615/a5570846/attachment.html>

Posted on the users mailing list.