<div dir="ltr">Hi Frederick,<br><br><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">If I'm not wrong you can take a similar approach to the one I took in<br>



my OpenAL wrapper on the Dumping Grounds page of the Gambit Wiki<br>
(<a href="http://dynamo.iro.umontreal.ca/wiki/images/2/2c/OpenAL.tar.gz" target="_blank">http://dynamo.iro.umontreal.ca/wiki/images/2/2c/OpenAL.tar.gz</a>). When<br>
I allocate a foreign struct, I attach a will to the object and in the<br>
will free the underlying C pointer when the foreign object is garbage<br>
collected. The implementation is at the bottom of the file labelled<br>
'foreign-macros.scm' in that tarball.<br></blockquote><div><br></div><div>Thanks a lot for pointing me to this!  I think it will be of great help.  If anyone knows about any other references to code by people that have worked at wrapping C structs/unions/types before I'd be very thankful to know.<br>
<br></div><div>(Heads up: while the file has extension .tar.gz, it's a non gzipped tarball.)<br><br></div><div>While I'll look into your approach in more depth, some preliminary comments:</div><div><br></div><div>
IIUC, the use case you talk about (handling a single struct in isolation) is handled extremely well by the default Gambit struct/union/type finalizer.  Even for arrays I'd just write a C finalizer that frees their memory; I explain below why.  I'm not sure why you are using wills here instead.<br>
<br></div><div>In any event, I'm convinced that if a library relies, to trigger finalization, on wills on foreign objects that are exposed to code written by others, then the library code can never really know for sure when the object will never be reachable again.  Consider:<br>
<br></div><div>> (define o (get-foreign-wrapper ...))  ; a will has been created for o, which will delete the underlying C data<br></div><div>> (define t (make-table weak-values: #t))<br></div><div>> (table-set! t 'o o)<br>
</div><div>> (set! o #f)<br></div><div>> (##gc)<br><br></div><div>At this point your foreign wrapper is not strongly reachable anymore.  The garbage collection has triggered your will, which has freed the C object wrapped by o.  But you can still<br>
<br></div><div>> (println (table-ref t 'o))<br><br></div><div>And get an access to invalid memory or a segmentation fault.<br></div><div><br>Note that the Gambit's ffi finalizers don't have this problem, since they only get called when your object is actually reclaimed.  Nobody can attach new finalizers to your foreign objects, so at the time they get called you know that nobody else will be messing with those[1].  For more discussion on this, see:<br>
</div><div><br><a href="https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-December/007322.html">https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-December/007322.html</a><br></div><div><br></div><div>[1] Well, you can do all sort of things from C code, but that is true for vanilla Scheme objects too.  I'm concerned about users getting weird crashes by doing perfectly valid things in Scheme code.<br>
</div><div><br></div></div></div></div></div>