How to free the array element

Huang, Ying huang_ying_caritas at 163.com
Wed Apr 1 12:44:20 UTC 2015


I am working on cl-gobject-introspection (https://github.com/andy128k/cl-gobject-introspection), a common lisp gobject introspection binding.  I am trying to use extended foreign type system of CFFI to implement foreign memory operations.  I have some questions about that.

I need to manipulate foreign array.  I tried to define an extended foreign array type because there is some features that is lacking in foreign-array-type.  I can access/assign element of foreign array with cffi:mem-ref and setf.  But I don't know how to free element of foreign array (the element type can be aggregated).

Then I checked foreign-array-type implementation of CFFI, and found there appears no way to free translated element of array too.  For example:

(defvar *string-array* (cffi:convert-to-foreign #("a" "b" "c") '(:array :string 3)))
(cffi:free-foreign-object *string-array* '(:array :string 3) t)

The foreign memory corresponding to string "a", "b" and "c" is not freed if my understanding were correct.

I think if there is something like cffi:mem-free like cffi:mem-ref, may be this can be resolved.  It can be something like.

(defun mem-free (ptr type param &optional (offset 0))
  (let* ((parsed-type (parse-type type))
         (ctype (canonicalize parsed-type)))
    (if (aggregatep parsed-type)
        (free-translated-object (inc-pointer ptr offset) parsed-type param)
        (free-translated-object (%mem-ref ptr ctype offset) parsed-type param))))

I think you guys may have better idea about how to resolve the question.

Best Regards,
Huang, Ying

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


More information about the cffi-devel mailing list