<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">2013/12/25 Estevo <span dir="ltr"><<a href="mailto:euccastro@gmail.com" target="_blank">euccastro@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Is it that.. you find it tricky to get in place a particular sequence of C FFI structure release function invocation on their GC???</div>


<div></div></div></blockquote><div><br></div></div><div>It's not tricky if you know what you're doing.  But if you are going to publish a library that exposes C bindings, you really don't want to have to be warning people against holding weak references to these foreign objects, lest they get a segfault or memory corruption.<br>

</div></div></div></div></blockquote><div><br>I have never been aware of anything like that. Can you provide a proof of concept example code that shows how weak references to a FFI object would be inconsistent?</div><div>

<br></div><div>I guess the inconsistencies possible would be that the FFI object would not GC so you run out of heap, or that the weak reference would drop too early - do you see any other type of inconsistency that would need to be evaluated?</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div></div><div>Something along the lines that if you have a C structure A that contains in it a reference to another C structure B, and you want Gambit's GC to automatically take care of this relationship in the sense that it will not release B before A??</div>


</div></blockquote><div><br></div></div><div dir="ltr">Yes, with the nuance that the C structure B is contained in A, not just referenced from it.  So it doesn't make sense to free B at all.  Another use case is that A is a union and B is another pointer to A's data.  You want to free A only once.  <br>


<br>But yes, the main point is that you want A to stick around while you have a reference to B.  This kind of sanity you take for granted in the Scheme world, thanks to garbage collection.  What I want is to be able to treat C objects similarly, without having to do unnecessary copies.<br>

</div></div></div></div>
</blockquote></div><br></div><div class="gmail_extra">Aha.</div><div class="gmail_extra"><br></div><div class="gmail_extra">You can implement this kind of behavior in Gambit (i.e. B is referenced to by or actually contained in A so you want freeing of B to be conditioned to the freeing of A happening before) yourself, for instance by</div>

<div class="gmail_extra"><br></div><div class="gmail_extra"> 1) ensuring you have a reference to B in some vector or alike, one that guaranteedly will stick around until A is freed. Also, you could</div><div class="gmail_extra">

<br></div><div class="gmail_extra"> 2) use the "refcount" facility that Gambit exports to the C world, to keep B's refcount +1 as long as A refers to it, and then have A's release procedure -1 its refcount on invocation.</div>

<div class="gmail_extra"><br></div><div class="gmail_extra">These would both be solid abstractions.</div><div class="gmail_extra"><br></div><div class="gmail_extra">What practical task are you solving?</div><div class="gmail_extra">

<br></div><div class="gmail_extra">Perhaps in the module you're writing you don't expose the C defines to your users anyhow, so for instance using declare not collect-dead-variables or what its name is, would make 1) work out well. Anyhow I guess 2) is general.</div>

<div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">There should be more solutions in the box too - any thoughts from other users?</div><div class="gmail_extra"><br></div><div class="gmail_extra">

<br></div><div class="gmail_extra">What are your thoughts now?</div><div class="gmail_extra"><br></div><div class="gmail_extra">Best regards,</div><div class="gmail_extra">Mikael</div><div class="gmail_extra"><br></div></div>