Hi,
On Mon, Jan 11, 2016 at 9:52 AM, Dan Rossi <
electr...@gmail.com> wrote:
> currently I have to do this
>
> info.data = _lib.malloc (1024) ;
>
> then free it up after
>
> _lib.free(info.data)
This is because ffi.new() returns a Python object that keeps the
memory alive as long as it exists. If you say for example ``info.data
= ffi.new("char[]", 1024)``, then that line is *bogus* in all cases:
the pointer value is copied into ``info.data``, but the Python object
returned by ffi.new() is immediately forgotten, and so the array of
chars is immediately deallocated.
There are two ways to fix it: one is like you do, with explicit calls
to malloc() and free(); the other way is to keep alive the Python
object manually, e.g.:
my_array = ffi.new("char[]", 1024)
info.data = my_array
and then make sure that the object stored in ``my_array`` is kept
alive as long as you need ``info.data`` to be valid. This might be as
simple as keeping it in a local or global variable that survives long
enough, or it might require saving it to a ``self._my_array``
attribute, for example.
A bientôt,
Armin.