The trickiest thing I found writing Gambit FFI code[1] was managing memory. After flailing around a bit scratching my head over memory leaks and crashes I found the best technique was to check the reference counts of objects as much as possible. Gambit doesn't appear to have a function to peek the refcount of a "STILL" allocated object, so I created a simple one in my code:
int _peek_refcount(___SCMOBJ s){ #define ___STILL_BODY_OFS 6 #define ___STILL_REFCOUNT_OFS 1 return ___UNTAG(s)[___BODY_OFS - ___STILL_BODY_OFS + ___STILL_REFCOUNT_OFS]; }
I then littered the code with assert calls. e.g:
assert(1 == _peek_refcount(myobj));
Hope this helps somebody,
Phil
[1] https://webmail.iro.umontreal.ca/pipermail/gambit-list/2007-February/001112....