From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: Re: make-c-struct and pointer->string Date: Sat, 30 Mar 2019 16:26:02 -0400 Message-ID: <877ecg85xm.fsf@netris.org> References: <20190326101419.340081fc@capac> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="173488"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: guile-devel To: David Pirotte Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Mar 30 21:27:39 2019 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hAKZm-000j0e-U2 for guile-devel@m.gmane.org; Sat, 30 Mar 2019 21:27:39 +0100 Original-Received: from localhost ([127.0.0.1]:40915 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hAKZl-0005qH-Oy for guile-devel@m.gmane.org; Sat, 30 Mar 2019 16:27:37 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:60922) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hAKZh-0005qA-9Q for guile-devel@gnu.org; Sat, 30 Mar 2019 16:27:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hAKZg-000123-B4 for guile-devel@gnu.org; Sat, 30 Mar 2019 16:27:33 -0400 Original-Received: from world.peace.net ([64.112.178.59]:60620) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hAKZg-00011m-58 for guile-devel@gnu.org; Sat, 30 Mar 2019 16:27:32 -0400 Original-Received: from mhw by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hAKZf-00019d-3h; Sat, 30 Mar 2019 16:27:31 -0400 In-Reply-To: <20190326101419.340081fc@capac> (David Pirotte's message of "Tue, 26 Mar 2019 10:14:47 -0300") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.112.178.59 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.org gmane.lisp.guile.devel:19870 Archived-At: Hi David, David Pirotte writes: > Hello, > > guile 2.2.4.1-cdb19 > > ,use (system foreign) > > ;; this fails > > scheme@(guile-user)> (make-c-struct (list '* '*) (list (string->pointer "= hello ") (string->pointer "there!"))) > $16 =3D # > scheme@(guile-user)> (parse-c-struct $16 (list '* '*)) > $17 =3D (# #) > scheme@(guile-user)> (map pointer->string $17) > $18 =3D ("?g?=D5=A3U" "`!?=D5=A3U") The Guile manual states: -- Scheme Procedure: string->pointer string [encoding] Return a foreign pointer to a nul-terminated copy of STRING in the given ENCODING, defaulting to the current locale encoding. The C string is freed when the returned foreign pointer becomes unreachable. Note the last sentence. When the returned foreign pointer (object) becomes unreachable, the C string is freed. The problem here is that you're not keeping a reference to those foreign pointer objects. If you look at the code in foreign.c, specifically 'scm_string_to_pointer' and 'scm_from_pointer', you'll see that the C pointer is wrapped within a heap-allocated foreign pointer object, and a finalizer is being set on that heap object. 'make-c-struct' copies the C pointers from those foreign pointer objects, but not not keep a reference to the objects themselves. In a later message, you wrote: > Following your explanation and example, I tried this and thought it would= work then, > but it also failed: > > GNU Guile 2.2.4.1-cdb19 > Copyright (C) 1995-2017 Free Software Foundation, Inc. > > Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. > This program is free software, and you are welcome to redistribute it > under certain conditions; type `,show c' for details. > > Enter `,help' for help. > scheme@(guile-user)> ,use (system foreign) > scheme@(guile-user)> (define str-1 "Hello") > scheme@(guile-user)> (define str-2 "there!") > scheme@(guile-user)> (make-c-struct (list '* '*) (list (string->pointer = str-1) (string->pointer str-2))) > $2 =3D # > scheme@(guile-user)> (parse-c-struct $2 (list '* '*)) > $3 =3D (# #) > scheme@(guile-user)> (map pointer->string $3) > $4 =3D ("" "`\v?\x02?U") Here are you specifically saving a reference to the Scheme string objects, but that's not what's needed. What's needed is to hold references to the foreign pointer objects returned by 'string->pointer' for as long as the C strings are needed. Regards, Mark