unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* dynamic-ffi: Responsibility for freeing returned pointers?
@ 2017-11-10 21:06 Tom Jakubowski
  2017-11-13 13:51 ` Ludovic Courtès
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Jakubowski @ 2017-11-10 21:06 UTC (permalink / raw)
  To: guile-user@gnu.org

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: dynamic-ffi: Responsibility for freeing returned pointers?
  2017-11-10 21:06 dynamic-ffi: Responsibility for freeing returned pointers? Tom Jakubowski
@ 2017-11-13 13:51 ` Ludovic Courtès
  0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2017-11-13 13:51 UTC (permalink / raw)
  To: guile-user

Hi Tom,

Tom Jakubowski <tjakubowski@oblong.com> skribis:

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

Indeed.

> This leaves open the question of freeing the original string returned
> by the tom_hello() C function.

It’s up to the programmer, as in C.  In this case, you could add an
explicit call to ‘free’, or do:

  (set-pointer-finalizer! ptr (dynamic-func "free" (dynamic-link)))

so that ‘free’ gets called eventually, when ‘ptr’ becomes unreachable.

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

I think it’s the right approach.

HTH,
Ludo’.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-11-13 13:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-10 21:06 dynamic-ffi: Responsibility for freeing returned pointers? Tom Jakubowski
2017-11-13 13:51 ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).