我是否需要释放通过CFFI调用的C函数返回的内存? [英] Do I need to free memory returned from a C function called via CFFI?

查看:120
本文介绍了我是否需要释放通过CFFI调用的C函数返回的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,其中包含一个函数text()返回一个新分配的字符串:

I have this example code that has a function text() returning a newly allocated string:

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()返回的字符串(如果CFFI在返回到Python代码后立即拥有该指针的所有权),我在文档中找不到任何内容.

However, I could not find anything in the docs whether I actually need to manually free() the returned string of if CFFI takes ownership of the pointer as soon as it's returned to Python code.

此外,如果我确实需要手动free()它,是否需要在我的 cdefs 中公开free(),或者CFFI是否为此提供了更好的方法?

Also, if I do need to manually free() it, do I need to expose free() in my cdefs or is does CFFI provide some nicer way for it?

推荐答案

摘录自使用指针,结构和数组,并引用正确的部分:

在C语言中返回指针,数组或结构类型的任何操作都会为您提供一个新鲜的cdata对象. 与原始"对象不同,这些新的cdata对象没有所有权

因此,您必须释放它,它无法承担所有权: 在C语言中,有许多函数可以返回指向内存中常量字符串的指针,例如,它不仅没有动态分配,也没有分配,或者根本无法修改.释放这些将是非常错误的.

Therefore you must free it, there is no way it can assume the ownership: In C there are many functions that return a pointer to a constant string in memory, that not only it is not dynamically allocated, it is not allocated, or modifiable at all, for example. Freeing those would be very erroneous.

对于free,文档中也显示以下内容:

Also for free, the documentation says the following:

另一种方法是声明并调用C malloc()和free()函数,或某些变体,例如mmap()和munmap().然后,您可以精确控制何时分配和释放内存.例如,将这两行添加到现有的ffibuilder.cdef()中:

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来注册自动清除:

Since it is very important that the correct C standard library free is used for a pointer returned by strdup you cannot rely CFFI to magically do the correct thing, but instead as you suspected, you should expose free(). You can also use the gc as suggested by Barmar to register an automatic clean up, if needed:

x = ffi.gc(x, lib.free)

这篇关于我是否需要释放通过CFFI调用的C函数返回的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆