[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: make-c-struct and pointer->string
From: |
David Pirotte |
Subject: |
Re: make-c-struct and pointer->string |
Date: |
Sun, 31 Mar 2019 07:38:30 -0300 |
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!)
pgpufo5G_hWpS.pgp
Description: OpenPGP digital signature
Re: make-c-struct and pointer->string, Mark H Weaver, 2019/03/30
- Re: make-c-struct and pointer->string,
David Pirotte <=