[cffi-devel] Type Translators and Callbacks

James Bielman jamesjb at jamesjb.com
Tue Nov 15 06:28:05 UTC 2005


Hi,

I'm finally able to spend some time on CFFI again, so I've been trying
to get caught up with the excellent progress Luis has made on the CFFI
type system.

I think I'm starting to understand how the new type translator stuff
works, but I'm a little wary of the :TO-C translation as used from
callbacks.  For example:

(defcallback return-a-string :string ()
  "Returns a string allocated in Lisp to C."
  (format nil "Hello, ~D." 42))

As Luis's comment in the C code portion of the callback tests indicates,
C code that calls this callback has no reliable way to deallocate the
memory allocated implicitly by the type translator.  The only way to
deallocate it is from Lisp via FOREIGN-STRING-FREE.

I think this is probably not too bad if we document it very clearly,
but it worries me.  Making type translators not apply to the return
value of callbacks would remove the possibility for a non-obvious memory
leak here.

Another option might be a :malloced-string type with a :TO-C translator
that explicitly malloc's the buffer---or just specify that
FOREIGN-STRING-ALLOC always uses malloc, I suppose.  Obviously that
wouldn't apply to possibly stack-allocated strings allocated by
WITH-FOREIGN-STRING...

In any other situation, it'd probably be better to use a result-type
of :POINTER.

Thoughts?

James





More information about the cffi-devel mailing list