From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: David Pirotte Newsgroups: gmane.lisp.guile.devel Subject: Re: make-c-struct and pointer->string Date: Sun, 31 Mar 2019 07:38:30 -0300 Message-ID: <20190331073830.41897916@capac> References: <20190326101419.340081fc@capac> <877ecg85xm.fsf@netris.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; boundary="Sig_/IIi.cI2ysefa00joXIjiAHm"; protocol="application/pgp-signature" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="144382"; mail-complaints-to="usenet@blaine.gmane.org" Cc: guile-devel To: Mark H Weaver Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Mar 31 12:39:00 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 1hAXrg-000bRI-7f for guile-devel@m.gmane.org; Sun, 31 Mar 2019 12:39:00 +0200 Original-Received: from localhost ([127.0.0.1]:45178 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hAXrf-00063g-2r for guile-devel@m.gmane.org; Sun, 31 Mar 2019 06:38:59 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:41437) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hAXrN-00063L-F7 for guile-devel@gnu.org; Sun, 31 Mar 2019 06:38:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hAXrM-0004cu-DV for guile-devel@gnu.org; Sun, 31 Mar 2019 06:38:41 -0400 Original-Received: from maximusconfessor.all2all.org ([79.99.200.102]:52338) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hAXrM-0004c7-25 for guile-devel@gnu.org; Sun, 31 Mar 2019 06:38:40 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by maximusconfessor.all2all.org (Postfix) with ESMTP id 5C5671BE00B4; Sun, 31 Mar 2019 12:38:37 +0200 (CEST) Original-Received: from maximusconfessor.all2all.org ([127.0.0.1]) by localhost (maximusconfessor.all2all.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SDG6XDW0egKi; Sun, 31 Mar 2019 12:38:37 +0200 (CEST) Original-Received: from capac (unknown [179.210.18.152]) by maximusconfessor.all2all.org (Postfix) with ESMTPSA id A37041BE00A9; Sun, 31 Mar 2019 12:38:36 +0200 (CEST) In-Reply-To: <877ecg85xm.fsf@netris.org> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 79.99.200.102 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:19871 Archived-At: --Sig_/IIi.cI2ysefa00joXIjiAHm Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Mark, Thanks for your help. > > ... > > 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") =20 > 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. Yes, and I was assuming that make-c-struct was holding a reference to 'its children', including pointers. > If you look at the code in foreign.c, specifically ... > 'make-c-struct' copies the C pointers from those foreign pointer objects= , but not > not keep a reference to the objects themselves. To me, this sounds very counter intuitive, actually, it sounds like a bug, make-c-struct should be holding a reference to the pointers it receives: i = seems to me that only when the c-struct itself becomes unreachable, that these point= ers could be freed? If I am wrong, and it seems I am (wrong), we really should think about writ= ing something about this in the manual (and explaining why would be nice). Cheers, David ps: in the last part of my email, I did precisely what you suggest in your answer, and showed that it works - if we hold a reference to these pointers 'by ourselves' - and asked if that was what is what was expected from us a= nd Neil Jerram to (also) find that 'very odd' > > ... > > Ok, let's hold-on to the pointers (which I thought would not be necessa= ry > > after holding-on c-struct and/or parsed): > > scheme@(guile-user)> (define p1 (string->pointer str-1)) > > scheme@(guile-user)> (define p2 (string->pointer str-2)) > > scheme@(guile-user)> (make-c-struct (list '* '*) (list p1 p2)) > > $19 =3D # > > scheme@(guile-user)> (parse-c-struct $19 (list '* '*)) > > $20 =3D (# #) > > scheme@(guile-user)> p1 > > $21 =3D # > > scheme@(guile-user)> p2 > > $22 =3D # > > scheme@(guile-user)> (map pointer->string $20) > > $23 =3D ("Hello" "there!") > > That worked, but I wonder if this is what is expected from us (users)? > Yes, that would seem very odd to me, as I thought that a 'pointer' really > was just an address. > (Hopefully some more light will be thrown when the maintainers are back > around again!) --Sig_/IIi.cI2ysefa00joXIjiAHm Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQEyBAEBCgAdFiEEhCJlRZtBM3furJHe83T9k6MFetcFAlygmKYACgkQ83T9k6MF etczIgf4s0QqzgZebnZFfALma2Pxnh8JOydwKovFJ6TRyyE0k2WOlqIZFBjGNvMj h/PcBtr+UnbF8KVL2iWS+SuKRGTH/zEqRXUHEpnxW5Hn3eoS/rrDfcUuN0VJjJ5C ZmxayxFhzGb7bc5lnavnxn8eCv7+NZZEqa/mDeS8QL+A34WDAk2l2zC3UaYcZYaJ fao+n3GjgBSyfyvS4NSNURLMHteAxIJGzMTImcW5QBAaUfJEqjINzD+6ikSj039K 0NGoj3bw3QzseLL7Nj0qg0dhdqwmKwQN1C61rcLjeAZIM89Wwf3SAcpM82yul13Q Lt1mxkaxvjNsyVQS/3Z7UAOExtDe =Rae2 -----END PGP SIGNATURE----- --Sig_/IIi.cI2ysefa00joXIjiAHm--