Hi,
On 7 November 2014 19:56, Alexey Poryadin <
alexey....@gmail.com> wrote:
> cdata = ffi.new("char[]", data) // cdata contain copy or ref of
> bytes in data?
With ffi.new(), the cdata contains a copy of the bytes in data.
> return cdata
This doesn't work: you can't have a callback returning a `char *` in
this way. The reason is that the cdata is garbage as soon as we leave
the function, and thus possibly deallocated, before the caller gets to
look at the bytes. You have the same problem as C functions that want
to return a `char *` string: you can't declare and fill a local array
of chars and then return a pointer to it.
You need to store the `cdata` object somewhere globally, and keep it
alive as long as the C caller of that callback may inspect it. How
long this is must be specified by this C code.
A bientôt,
Armin.