gpt4 book ai didi

python - 我是否需要释放通过 CFFI 调用的 C 函数返回的内存?

转载 作者:太空狗 更新时间:2023-10-29 16:10:23 26 4
gpt4 key购买 nike

我有这个示例代码,它有一个函数 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com