guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dynamic-ffi: Responsibility for freeing returned pointers?


From: Tom Jakubowski
Subject: dynamic-ffi: Responsibility for freeing returned pointers?
Date: Fri, 10 Nov 2017 21:06:33 +0000

Hi!

Say I have a C function like this:

char *tom_hello() {
  return strdup("hello!");
}

And, in Guile land, I make a procedure c-tom-hello out of it using
dynamic-ffi and wrap it in a function that returns a Scheme string:

(define exe (dynamic-link))
(define c-tom-hello (pointer->procedure '* (dynamic-func "tom_hello" exe)
'())
(define (tom-hello)
  (let ((str (pointer->string c-tom-hello)))
    ;; free pointer?
    str))

My assumption (supported by reading strings.c, but not in any docs I can
find) is that pointer->string makes a copy of the string it's passed. This
leaves open the question of freeing the original string returned by the
tom_hello() C function.

Is the usual technique here to use dynamic-ffi to make a procedure that's
bound to C's free(), and call that on the C string after making the Guile
string from it? It seems like the obvious choice, I'd just like to be sure
I'm not missing some higher level approach I should use instead.

Thanks!
Tom


reply via email to

[Prev in Thread] Current Thread [Next in Thread]