From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.user Subject: Re: dynamic-ffi: Responsibility for freeing returned pointers? Date: Mon, 13 Nov 2017 14:51:39 +0100 Message-ID: <87po8mthg4.fsf@gnu.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1510581152 6818 195.159.176.226 (13 Nov 2017 13:52:32 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 13 Nov 2017 13:52:32 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Nov 13 14:52:25 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eEF9z-0001Os-Tg for guile-user@m.gmane.org; Mon, 13 Nov 2017 14:52:24 +0100 Original-Received: from localhost ([::1]:54501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eEFA7-0004Kv-Bd for guile-user@m.gmane.org; Mon, 13 Nov 2017 08:52:31 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41690) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eEF9b-0004In-BQ for guile-user@gnu.org; Mon, 13 Nov 2017 08:52:00 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eEF9Y-0007UO-3T for guile-user@gnu.org; Mon, 13 Nov 2017 08:51:59 -0500 Original-Received: from [195.159.176.226] (port=38448 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eEF9X-0007SR-SA for guile-user@gnu.org; Mon, 13 Nov 2017 08:51:56 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1eEF9M-0007p1-FZ for guile-user@gnu.org; Mon, 13 Nov 2017 14:51:44 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 45 Original-X-Complaints-To: usenet@blaine.gmane.org X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 23 Brumaire an 226 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu Cancel-Lock: sha1:DsXIgXwCU3nrj6OS5Q2qVSUy5VA= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14255 Archived-At: Hi Tom, Tom Jakubowski 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’.