[plt-scheme] Binding C-Structs Containing Fixed-Size Arrays with the FFI

From: Henk Boom (lunarc.lists at gmail.com)
Date: Mon Jan 26 16:09:30 EST 2009

2009/1/26 Eli Barzilay <eli at barzilay.org>:
> On Jan 26, Henk Boom wrote:
>> 2009/1/26 Jakub Piotr C?apa <jpc-ml at zenburn.net>:
>> > You may want to check these threads:
>> >
>> > http://thread.gmane.org/gmane.lisp.scheme.plt/12987
>> > http://thread.gmane.org/gmane.lisp.scheme.plt/28412/focus=28509
>> > http://list.cs.brown.edu/pipermail/plt-scheme/2007-March/016671.html
>> Thank you. I've taken a look, and come up with a solution, but I'm
>> not sure if it's correct.
> It looks fine, and better than the previous solution, I think.
>> Specifically, I don't know how the garbage collector will interact
>> with it: will the the cvector created by make-cvector* properly
>> "own" the buffer it references?
> At the scheme side -- as long as you're doing the allocations -- there
> shouldn't be any problems since it's all handeled transparently by the
> GC.

I'm just worried because the cvector returned by (foo-j s) points to
the middle of the structure, not to the beginning, and I'm not sure if
the garbage collector can keep track of it.

> But in general, one good property of a moving GC is that memory
> bugs pop up fast: just run `collect-garbage' in random places and see
> if it doesn't crash.
> --
>          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:

Ah, I see, I hadn't thought of that. Unfortunately if I replace the
cvector-set! line with:

(cvector-set! (foo-j s) (begin (collect-garbage) 0) 42.)

I get the output:

i: 10.0 20.0 30.0 40.0
j: 11.0 22.0 33.0 44.0
Seg fault (internal error during gc) at 0x1

Any idea why?


Posted on the users mailing list.