HI Matthias,
maybe the fog about cffi magic is beginning to disappear for me ;-) Many thanks.
If I have understood (not sure at this point) :
I can transform directly my C array res[0] of length nres[0] into a buffer with :
buf = ffi.buffer(res[0],ffi.sizeof("double")*nres[0])
then get a numpy array with :
y = np.from_buffer(buf, dtype=float)
So I don't need a copy of the array res[0] (good).
After that, as soon as y and buf are not any more referenced, the garbage collector of python can free the underlying
objects but won't be able to free the memory used by my C array res[0] (as Python has not the ownership on it) ?
For that purpose (avoid memory leaks...), I should use ffi.gc before these 2 instructions, something like :
new_res = ffi.gc(res[0], C.free_mem)
buf = ffi.buffer(new_res, ffi.sizeof("double")*nres[0])
y = np.from_buffer(buf, dtype=float)
and now python has the ownership onto newres and will be able to free the memory used by the C array res[0] ?
I have tested this (using big arrays and top in another console to see the memory usage of the python3 process involved)
and effectively it seems to work (no derive).
Lastly as the C code uses stdlib malloc, is my wrapper (free_mem) on to stdlib free, necessary ? That is, is it possible to
tell ffi.gc that I want to use stdlib free directly ?
Well it will be a long time that I didn't face with link of various libs in a code, I see that the API mode
seems interesting (I have just try ABI currently) and discover that it seems possible with the distutils
to create a python package containing c code in a independent OS way (that is with compilation of the
C code and the interface C code without using other tools (like autotools, cmake, ...), looks really interesting
(a long time ago I wrote interfaces on sparse solvers (umfpack, taucs cholesky) for scilab but my interface
build was only working on linux...).
Cheers
Bruno