Hi,
If I have a C function which allocates memory for a C string and returns a pointer to this memory (return type char *). How do I deallocate this memory? Should I use `foreign-string-free' or `foreign-free'.
I have been meaning to look into this. Right now, you'd have to use something like (foreign-funcall "free" :pointer <your-pointer>)
I don't understand why there's something unclear here. I thought that's why :string+ptr had been invented.
This is exactly the pattern of :string+ptr, and it's embodied in tests/misc-types.lisp:deftest misc-types.string+ptr: a) return string and b) (foreign-free pointer) Is that testcase broken? "Just" non-portable?
Well, since foreign-free's specification is to free only things allocated with foreign-alloc, maybe CFFI must export the general C free() as well? The :string+ptr type should then documented this.
Note that malloc()/free() is fine for a UNIX shared library. It's somewhat meaningless in the context of a DLL on MS-Windows. But there, one will probably not encounter such a protocol and will have to call the resource free'ing function via the FFI as well.
Regards, Jorg Hohle.
On 12/19/05, Hoehle, Joerg-Cyril Joerg-Cyril.Hoehle@t-systems.com wrote:
This is exactly the pattern of :string+ptr, and it's embodied in tests/misc-types.lisp:deftest misc-types.string+ptr: a) return string and b) (foreign-free pointer) Is that testcase broken? "Just" non-portable?
I need to investigate this a bit further, but I think that right now that testcase is indeed non-portable. Since not all Lisps use the standard malloc/free to (de)allocate foreign memory. Allegro uses aclmalloc(), for instance, so the standard free() on the C side won't quite work on memory allocated by cffi:foreign-alloc.
Well, since foreign-free's specification is to free only things allocated with foreign-alloc, maybe CFFI must export the general C free() as well?
Yes, I think so. The idea is to add an extra parameter to foreign-alloc and foreign-free to specify whether the memory should be allocated with malloc (and deallocated with free) or not.
-- Luís Oliveira http://student.dei.uc.pt/~lmoliv/ Equipa Portuguesa do Translation Project http://www.iro.umontreal.ca/translation/registry.cgi?team=pt