guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C calling Scheme and garbage collection


From: David Pirotte
Subject: Re: C calling Scheme and garbage collection
Date: Sun, 30 Jun 2019 09:17:32 -0300

Hello,

>...
> In my experience, it's definitely not ok to capture a pointer to a
> scheme object and store it for later use without protecting the scheme
> object from gc by holding a reference.

I recently had a similar conversation, here is one of the emails of the thread, 
the
one that lists a series of examples, and out of those, only the last example 
works:

        https://lists.gnu.org/archive/html/guile-devel/2019-03/msg00013.html

In that last example, I am holding a reference to both the data and their 
pointer,
but it seemed to me, after further local tests, that it is sufficient to hold 
on a
reference to the pointers, not the pointers and the data:

scheme@(guile-user)> ,use (system foreign)
scheme@(guile-user)> (define ptr-1 (string->pointer "Hello"))
scheme@(guile-user)> (define ptr-2 (string->pointer "there!"))
scheme@(guile-user)> (make-c-struct (list '* '*) (list ptr-1 ptr-2))
$2 = #<pointer 0x5623b599a350>
scheme@(guile-user)> (parse-c-struct $2 (list '* '*))
$3 = (#<pointer 0x5623b51b1110> #<pointer 0x5623b591e6f0>)
scheme@(guile-user)> (map pointer->string $3)
$4 = ("Hello" "there!")
scheme@(guile-user)> (gc)
scheme@(guile-user)> (map pointer->string $3)
$5 = ("Hello" "there!")

Is this correct, or am I just lucky here? What is certain is that holding a
reference to the data (only) does not work, as shown in the examples
in the 'pointed' email ...

David

Attachment: pgp3WXIJnymFz.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]