Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError:获取所有权或释放cdata时免费_Python_Python Cffi - Fatal编程技术网

Python AttributeError:获取所有权或释放cdata时免费

Python AttributeError:获取所有权或释放cdata时免费,python,python-cffi,Python,Python Cffi,如果我想拥有一个在C中是malloced的指针的所有权,并说使用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

如果我想拥有一个在C中是
malloc
ed的指针的所有权,并说使用
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
作为lib的
cdef
s的一部分公开,就像在Python端访问任何其他函数一样。放置
无作废(作废*ptr)ffi.cdef
调用中的code>和
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)
从tempfile导入临时目录
从weakref导入WeakKeyDictionary
来自cffi进口FFI
公共_标题=”“
类型定义结构{
int32_t长度;
双*值;
}我的结构;
"""
#外国金融机构
ffi=ffi()
ffi.cdef(通用标题+“”“
int func(我的结构*);
无空隙(空隙*ptr);//长度=3;
输入->值=值;
返回0;
}
""")
临时目录()作为临时目录:
lib_path=ffi.compile(tmpdir=temp_dir)
lib=ffi.dlopen(lib_路径)
func=lib.func
#使用图书馆
my_struct=ffi.new('my_struct*'))
func(我的结构)
#取得malloced成员的所有权
全局_weakkey=weakkey字典()
全局_weakkey[my_struct]=ffi.gc(my_struct.values,lib.free)

文档没有强调这一点,但是您需要公开
free
作为lib的
cdef
的一部分,就像在Python端访问任何其他函数一样。放置
无作废(作废*ptr)ffi.cdef
调用中的code>和
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)
从tempfile导入临时目录
从weakref导入WeakKeyDictionary
来自cffi进口FFI
公共_标题=”“
类型定义结构{
int32_t长度;
双*值;
}我的结构;
"""
#外国金融机构
ffi=ffi()
ffi.cdef(通用标题+“”“
int func(我的结构*);
无空隙(空隙*ptr);//长度=3;
输入->值=值;
返回0;
}
""")
临时目录()作为临时目录:
lib_path=ffi.compile(tmpdir=temp_dir)
lib=ffi.dlopen(lib_路径)
func=lib.func
#使用图书馆
my_struct=ffi.new('my_struct*'))
func(我的结构)
#取得malloced成员的所有权
全局_weakkey=weakkey字典()
全局_weakkey[my_struct]=ffi.gc(my_struct.values,lib.free)