AttributeError: free when taking ownership or freeing cdata
AttributeError: free when taking ownership or freeing cdata
如果我想获得在 C 中 malloc
ed 的指针的所有权,docs for the Python cffi
package and 说使用 ffi.gc
和 lib.free
作为析构函数。但是,当我这样做时,我在 lib.free
的调用中得到 AttributeError: free
。 lib.free
定义在哪里?
from tempfile import TemporaryDirectory
from weakref import WeakKeyDictionary
from cffi import FFI
common_header = """
typedef struct {
int32_t length;
double* values;
} my_struct;
"""
# FFI
ffi = FFI()
ffi.cdef(common_header + """
int func(my_struct*);
""")
ffi.set_source('_temp', common_header + """
int func(my_struct *input) {
double* values = malloc(sizeof(double) * 3);
input->length = 3;
input->values = values;
return 0;
}
""")
with TemporaryDirectory() as temp_dir:
lib_path = ffi.compile(tmpdir=temp_dir)
lib = ffi.dlopen(lib_path)
func = lib.func
# Using the library
my_struct = ffi.new('my_struct*')
func(my_struct)
# Taking ownership of the malloced member
global_weakkey = WeakKeyDictionary()
global_weakkey[my_struct] = ffi.gc(my_struct.values, lib.free)
# AttributeError: free
文档没有强调这一点,但您需要将 free
作为库的 cdef
的一部分公开,就像任何其他函数一样,以便在 [=19] 上访问它=] 边。将 void free(void *ptr);
放在对 ffi.cdef
的调用中, free
函数将在编译后通过 lib.free
可用:
from tempfile import TemporaryDirectory
from weakref import WeakKeyDictionary
from cffi import FFI
common_header = """
typedef struct {
int32_t length;
double* values;
} my_struct;
"""
# FFI
ffi = FFI()
ffi.cdef(common_header + """
int func(my_struct*);
void free(void *ptr); // <-- Key to lib.free working later
""")
ffi.set_source('_temp', common_header + """
int func(my_struct *input) {
double* values = malloc(sizeof(double) * 3);
input->length = 3;
input->values = values;
return 0;
}
""")
with TemporaryDirectory() as temp_dir:
lib_path = ffi.compile(tmpdir=temp_dir)
lib = ffi.dlopen(lib_path)
func = lib.func
# Using the library
my_struct = ffi.new('my_struct*')
func(my_struct)
# Taking ownership of the malloced member
global_weakkey = WeakKeyDictionary()
global_weakkey[my_struct] = ffi.gc(my_struct.values, lib.free)
如果我想获得在 C 中 malloc
ed 的指针的所有权,docs for the Python cffi
package and ffi.gc
和 lib.free
作为析构函数。但是,当我这样做时,我在 lib.free
的调用中得到 AttributeError: free
。 lib.free
定义在哪里?
from tempfile import TemporaryDirectory
from weakref import WeakKeyDictionary
from cffi import FFI
common_header = """
typedef struct {
int32_t length;
double* values;
} my_struct;
"""
# FFI
ffi = FFI()
ffi.cdef(common_header + """
int func(my_struct*);
""")
ffi.set_source('_temp', common_header + """
int func(my_struct *input) {
double* values = malloc(sizeof(double) * 3);
input->length = 3;
input->values = values;
return 0;
}
""")
with TemporaryDirectory() as temp_dir:
lib_path = ffi.compile(tmpdir=temp_dir)
lib = ffi.dlopen(lib_path)
func = lib.func
# Using the library
my_struct = ffi.new('my_struct*')
func(my_struct)
# Taking ownership of the malloced member
global_weakkey = WeakKeyDictionary()
global_weakkey[my_struct] = ffi.gc(my_struct.values, lib.free)
# AttributeError: free
文档没有强调这一点,但您需要将 free
作为库的 cdef
的一部分公开,就像任何其他函数一样,以便在 [=19] 上访问它=] 边。将 void free(void *ptr);
放在对 ffi.cdef
的调用中, free
函数将在编译后通过 lib.free
可用:
from tempfile import TemporaryDirectory
from weakref import WeakKeyDictionary
from cffi import FFI
common_header = """
typedef struct {
int32_t length;
double* values;
} my_struct;
"""
# FFI
ffi = FFI()
ffi.cdef(common_header + """
int func(my_struct*);
void free(void *ptr); // <-- Key to lib.free working later
""")
ffi.set_source('_temp', common_header + """
int func(my_struct *input) {
double* values = malloc(sizeof(double) * 3);
input->length = 3;
input->values = values;
return 0;
}
""")
with TemporaryDirectory() as temp_dir:
lib_path = ffi.compile(tmpdir=temp_dir)
lib = ffi.dlopen(lib_path)
func = lib.func
# Using the library
my_struct = ffi.new('my_struct*')
func(my_struct)
# Taking ownership of the malloced member
global_weakkey = WeakKeyDictionary()
global_weakkey[my_struct] = ffi.gc(my_struct.values, lib.free)