Ownership of nested structs

Issue #36 wontfix
created an issue

In the case of a structure embedding another structure, both sharing the same memory area, a reference to the inner structure will not prevent the outer structure from being garbage-collected. Examples are struct stat (containing structures for modification times) and plain C implementations of class inheritance or mixins like linked list membership.

There's some documentation indicating that CFFI can't be aware of pointers created by C code, but when dereferencing outer.inner, CFFI creates a CData object that can be managed. I propose that this pointer should keep the outer struct alive. This will ensure consistency, in that an obj.member reference will stay available both in the case of nested structs and structs allocated by malloc or ffi.new.

Comments (2)

  1. Armin Rigo

    It will be difficult to convince me. If we go down this road, then a pointer to an array in a struct should also keep the struct alive, and then a pointer to a struct that belongs to an array in that struct, and so on and so forth. The rule now is simpler: if you need memory allocated by ffi.new() to stay alive, you have to keep the original object alive.

  2. Armin Rigo

    Won't fix: the current rules at least are simple. The rules you propose are a partial mixture of the ctypes' rules. I don't think going there is a really good idea, but I certainly think going there halfway is a bad idea.

  3. Log in to comment