- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这个示例代码,它有一个函数 text()
返回一个新分配的字符串:
ffi_test = FFI()
ffi_test.set_source('_test', '''
char* test() { return strdup("hello world"); }
''')
ffi_test.cdef('''
char* test();
void free(void *);
''')
ffi_test.compile(verbose=True)
这很好用:
In [1]: from _test import ffi, lib
In [2]: x = lib.test()
In [3]: ffi.string(x)
Out[3]: b'hello world'
In [4]: lib.free(x)
但是,我在文档中找不到任何内容,我是否真的需要手动 free()
if CFFI 返回的字符串一旦返回到 Python 代码就获得指针的所有权。
此外,如果我确实需要手动 free()
它,我需要在我的 cdefs 中公开 free()
还是CFFI 是否为此提供了一些更好的方法?
最佳答案
来自 Working with pointers, structures and arrays 上的文档,并引用正确的部分:
Any operation that would in C return a pointer or array or struct type gives you a fresh cdata object. Unlike the “original” one, these fresh cdata objects don’t have ownership
因此您必须释放它,它无法取得所有权:在 C 中,有许多函数返回指向内存中的常量字符串 的指针,例如,它不仅不是动态分配的,而且根本不是分配的,或者根本不可修改的。释放它们将是非常错误的。
同样对于 free
,文档说明如下:
An alternative is to declare and call the C malloc() and free() functions, or some variant like mmap() and munmap(). Then you control exactly when the memory is allocated and freed. For example, add these two lines to your existing ffibuilder.cdef():
void *malloc(size_t size);
void free(void *ptr);
由于 正确 C 标准库 free
用于 strdup
返回的指针非常重要,因此您不能依赖 CFFI 神奇地做正确的事情,但正如您所怀疑的那样,您应该公开 free()
。如果需要,您还可以按照 Barmar 的建议使用 gc
注册自动清理:
x = ffi.gc(x, lib.free)
关于python - 我是否需要释放通过 CFFI 调用的 C 函数返回的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47080411/
好的,我尝试了这种从外国翻译过来的方法,它确实有效我在我的库中的 structs.lisp 文件中定义了这些,它在我所有其他依赖项之前首先加载 (cffi:defcstruct (cv-size :c
我正在尝试使用来自 SBCL 的 BLAS/LAPACK 库(特别是试图让 LLA 包运行)。我在加载 BLAS 共享库时遇到了很多麻烦;最终我发现它无法加载其依赖库。最终我能够通过手动加载它的所有依
有一种方法可以用字典初始化结构: fooData= {'y': 1, 'x': 2} fooStruct = ffi.new("foo_t*", fooData) fooBuffer = ffi.bu
我想使用 stat(2)来自 Common Lisp。 我已经定义了 stat 函数使用的结构: (cffi:defctype mode_t :unsigned-int) (cffi:defctype
在普通的 lisp 中,是否有推荐的外来类型的命名约定? (和一般类型)?例如: (cffi:defctype glyph-index-t :uint32) (cffi:defcstruct Poin
假设我在 cffi 下定义了一个枚举: from cffi import FFI ffi = FFI() ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEAR
IplImage struct documentation描述了 IplROI* roi 槽,它似乎是指向核心 types_c.h 头文件中定义的 IplROI struct 的指针: typede
我使用 cffi 模块来包装一个简单的 C 代码。问题是,我需要添加一个标志以使其编译 (std=c99)。目前我有类似的东西: from cffi import FFI ffibuilder = F
这是一个 C 语言的函数: union Example { int number; void *pointer; }; void return_a_value (union Examp
我是 Common Lisp 的新手,并对其进行了一些实验。我正在努力获得对 Windows 剪贴板的一些访问权限,然后我找到了这个引用: https://groups.google.com/foru
我实际上正在编写一个带有 cffi 模块的 python 程序来测试我的 C/ASM 库,我设法让它工作。但我不知道如何访问字符串中间字符的地址以将其传递给我的 lib 函数。例如: def my_b
我的最终目标是编译系统 FLEXI-TRIVIAL-DIRED ( http://common-lisp.net/project/ftd/ ),但我遇到了麻烦,因为我在任何地方都找不到所需的软件包之一
如果我使用 cffi.FFI.new 创建一个新结构,如何将其传递给具有相同结构定义的不同 FFI 中的函数? 我有一个基本的 C 结构,我通过 cffi 包在 Python 中使用它,我想将其传递给
我正在尝试使用 pip3 在 fedora23 工作站上安装 cffi。但我收到以下错误。 pip3 install cffi Collecting cffi Using cached cffi-
我想使用 cffi(如果必须,甚至使用 ctypes)从 Linux 上的 Python 3 访问 C ABI。该 API 由许多 .so 文件实现(我们称它们为 libA.so、libB.so 和
我想公开一个对象的缓冲区协议(protocol),就像in this example Cython 文档,但是我需要使用 CFFI 来执行此操作而且我找不到任何公开缓冲区协议(protocol)的示例
我正在学习如何将用 C 编写的代码包含到 Python 中,因为我有一个用于 Microchip 设备的 API,这对于我希望通过为其添加一个 Python 包装器来让我将来的生活更轻松,这将使我能够
我的C++代码是 StructureEx* obj; // structure functionEx((void**)&obj); 我的职能是 int functionEx(void** obj);
尝试在我的 linux 机器上的 python 3.4.3 上安装 cffi-1.7.0 源代码。出现以下错误。 $ python setup.py 安装 cc1:错误:无法识别的命令行选项“-Wno
我有一些 C 库,我想使用 CFFI 在 Python 中访问。构建库后,我得到了 2 个文件:$HOME/libcint/include/cint.h 和 $HOME/libcint/lib/lib
我是一名优秀的程序员,十分优秀!