Hi,
When allocating memory using ffi.new the cdata object returned owns the memory:
procent_list = ffi.new('struct procentry64[1]')
Now procent_list is a cdata object which owns the allocated memory.
However when accessing the first item the new cdata object does not
keep the memory alive:
procent = procent_list[0]
del procent_list # dealocates memory
procent.pi_pid # random stuff
This seems all fine and logical, the thing which tripped me up however
was the different behaviour when the initial ffi.new call used a
pointer instead:
procent_list = ffi.new('struct procentry64*')
procent = procent_list[0]
del procent_list
procent.pi_pid # all fine
Given how cffi kind of treats the 1-item array and pointer the same on
the surface of it (both allocate the space for one item which is not
the same as C does and both need indexing with [0] to get the item)
this sudden change in how memory is owned seems rather surprising.
And while the documentation does mention this it is not the most
obvious to find.
Is there any reason why the cdata items of an array do not keep the
whole array alive? Would that not make the API more consistent?
Regards,
Floris
--
Debian GNU/Linux -- The Power of Freedom
www.debian.org |
www.gnu.org |
www.kernel.org