guile-devel
[Top][All Lists]
Advanced

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

Re: make-c-struct and pointer->string


From: Mark H Weaver
Subject: Re: make-c-struct and pointer->string
Date: Mon, 01 Apr 2019 04:50:32 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi David,

David Pirotte <address@hidden> writes:

>>  '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?

In my previous email I described some practical difficulties with
implementing this suggestion, but I should rather emphasize a more
important point:

'make-c-struct' is ultimately meant for passing data to C, and in
general you should assume that Boehm GC will _not_ see references from C
data structures to your GC-allocated objects.

In fact, Boehm GC does _not_ scan objects allocated using C 'malloc',
nor C++ 'new', nor 'make-c-struct'.  So, for purposes of garbage
collection, you should normally assume that passing pointers to C land
is equivalent to passing them to a block hole.

In general, when passing GC-allocated objects to C code, unless the C
code in question was written with Boehm GC in mind, you must always
think about how long the object must be kept alive, and arrange to keep
a reference to it for at least that long, and preferably no longer than
necessary or else you'll have space leaks.

I'm open to suggestions of how to make this better, although I'm also
fairly confident that interfacing between GC-managed and non-GC-managed
code is a thorny problem with no silver bullets.

       Mark



reply via email to

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