[cffi-devel] A question about memory management and garbage collection

Nitralime nitralime at googlemail.com
Sun May 8 12:39:41 UTC 2011


Hi folks!

I have the follwoing situation:

A foreign function call returns a pointer to a "table" object.
This is an opaque object that can only be manipulated by
the APIs provided by the foreign library. To avoid memory leaks
the "table" objects should by destroyed by a call  to "destroy_table"
function if they are not used anymore.

I have wrapped the "table" object and its corresponding API functions
in a CLOS object with appropriate methods. Instances of this CLOS object
will be returned by function calls on the Lisp side.

The CLOS instance contains a reference to a C "table" object
(allocated on C-heap by foreign library). If a caller in a Lisp program
ignores the returned "table" object, it should be garbage collected
as expected.

My questions are now:
     1) Will it be really garbage collected?
     2) If the answer is in affirmative, is it garbage collected correctly
        (no potential memory leaks)?

Please note that the C API offers as mentioned above
its own dedicated function for that purpose.

What would be a correct solution to avoid memory leaks in this situation?
Using a finalizer seems to be a possible way to go. But I'm not sure how
this can be done by just using the finalizer parameters "object" and 
"function"
where "function" can't reliablely access "object" (cf. documentation of 
finalizer
in "trivial-garbage"  package)!!

Any help and feedback is very much appreciated!

Regards
Nik


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20110508/7d59d753/attachment.html>


More information about the cffi-devel mailing list