When I'm writing C libraries for interface to Guile (either as
wrappers or from scratch), a problem arises, and I'd like someone to
point out what the correct solution is.
Suppose your C library has data types Foo and Bar.
A Foo can contain a pointer to a Bar. You write the functions
Clib-make-new-foo, Clib-make-new-bar, and
Clib-foo-set-pointer-to-bar.
Now suppose one of your users does something like this:
(define toplevelfoo (Clib-make-new-foo))
(let ((temp-bar (Clib-make-new-bar)))
(Clib-foo-set-pointer-to-bar toplevelfoo temp-bar))
(gc)
Now, the bar you allocated with Clib-make-bar doesn't have any scheme
references to it any more, so it gets garbage collected, because the
garbage collector doesn't know that there is a pointer to it inside
toplevelfoo. And the next time the program tries to look for
toplevelfoo->bar it will seg fault.
Of course, you can solve this by not freeing the C-bar when the
Scheme-bar is collected, but that entails a memory leak.
What is the "right way" to solve this dilemma? Is there one?
-Dan Risacher