unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: David Pirotte <david@altosw.be>
To: Mark H Weaver <mhw@netris.org>
Cc: guile-devel <guile-devel@gnu.org>
Subject: Re: make-c-struct and pointer->string
Date: Sun, 31 Mar 2019 07:38:30 -0300	[thread overview]
Message-ID: <20190331073830.41897916@capac> (raw)
In-Reply-To: <877ecg85xm.fsf@netris.org>

[-- Attachment #1: Type: text/plain, Size: 2954 bytes --]

Hi Mark,

Thanks for your help.

> > ...
> > scheme@(guile-user)> (make-c-struct (list '* '*) (list (string->pointer "hello
> > ") (string->pointer "there!"))) $16 = #<pointer 0x55a3d54d54d0>
> > scheme@(guile-user)> (parse-c-struct $16 (list '* '*))
> > $17 = (#<pointer 0x55a3d5d12170> #<pointer 0x55a3d5d0a640>)
> > scheme@(guile-user)> (map pointer->string $17)
> > $18 = ("?g?գU" "`!?գU")  

> 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 pointers could
be freed?

If I am wrong, and it seems I am (wrong), we really should think about writing
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 and
	Neil Jerram to (also) find that 'very odd'

> > ...
> > Ok, let's hold-on to the pointers (which I thought would not be necessary
> > 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 = #<pointer 0x5584467a5d70>
> > scheme@(guile-user)> (parse-c-struct $19 (list '* '*))
> > $20 = (#<pointer 0x5584468b0190> #<pointer 0x5584468b4430>)
> > scheme@(guile-user)> p1
> > $21 = #<pointer 0x5584468b0190>
> > scheme@(guile-user)> p2
> > $22 = #<pointer 0x5584468b4430>
> > scheme@(guile-user)> (map pointer->string $20)
> > $23 = ("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!)

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 484 bytes --]

  reply	other threads:[~2019-03-31 10:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-26 13:14 make-c-struct and pointer->string David Pirotte
2019-03-28 15:15 ` Amirouche Boubekki
2019-03-28 15:42   ` Neil Jerram
2019-03-29  8:48     ` David Pirotte
2019-03-29  9:46       ` David Pirotte
2019-03-29 19:08         ` Neil Jerram
2019-03-28 15:48   ` David Pirotte
2019-03-30 20:26 ` Mark H Weaver
2019-03-31 10:38   ` David Pirotte [this message]
2019-04-01  6:23     ` Mark H Weaver
2019-04-01  8:50     ` Mark H Weaver
2019-04-02 18:03       ` David Pirotte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190331073830.41897916@capac \
    --to=david@altosw.be \
    --cc=guile-devel@gnu.org \
    --cc=mhw@netris.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).