在使用 ctypes 处理结合了 Python 3 代码和 C++ 代码的复杂程序时,我发现了一个内存泄漏,可以通过下面的精简示例轻松重现。
我的 C++ 代码使用回调函数创建了一个 Python 对象。接下来,它调用 Python 对象的另一个回调,它只返回它的参数。第二个回调导致对象的引用计数增加。因此,该对象永远不会被垃圾回收。
这是 Python 代码(文件 bug.py):
import ctypes
CreateObjectCallback = ctypes.CFUNCTYPE( ctypes.py_object )
NoopCallback = ctypes.CFUNCTYPE( ctypes.py_object, ctypes.py_object )
lib = ctypes.cdll.LoadLibrary("./libbug.so")
lib.test.restype = ctypes.py_object
lib.test.argtypes = [ CreateObjectCallback, NoopCallback ]
class Foo:
def __del__(self):
print("garbage collect foo");
def create():
return Foo()
def noop(object):
return object
lib.test(CreateObjectCallback(create), NoopCallback(noop))
这是 C++ 代码(文件 bug.cpp):
#include <python3.6m/Python.h>
#include <iostream>
#include <assert.h>
extern "C" {
typedef void *(*CreateObjectCallback)();
typedef void *(*NoopCallback)(void *arg);
void *test(CreateObjectCallback create, NoopCallback noop)
{
void *object = create();
std::cerr << "ref cnt = " << ((PyObject*)(object))->ob_refcnt << std::endl;
object = noop(object);
std::cerr << "ref cnt = " << ((PyObject*)(object))->ob_refcnt << std::endl;
return object;
}
}
下面是我用来编译和运行的命令:
g++ -O3 -W -Wextra -Wno-return-type -Wall -Werror -fPIC -MMD -c -o bug.o bug.cpp
g++ -shared -Wl,-soname,libbug.so -o libbug.so bug.o
python3 bug.py
输出是:
ref cnt = 1
ref cnt = 2
换句话说,对 noop 函数的调用错误地增加了引用计数,并且 Foo 对象没有被垃圾回收。如果不调用 noop 函数,Foo 对象将被垃圾回收。预期的输出是:
ref cnt = 1
ref cnt = 1
garbage collect foo
这是一个已知问题吗?有谁知道解决方法或解决方案?这是由 ctypes 中的错误引起的吗?
最佳答案
您正在传递 Python 对象。您的一个对象被传递到您的 C 代码中,但没有传递出去,因此您负责该引用计数。这是可行的,但我已将 void*
更改为 PyObject*
,因为它们就是这样:
#include <Python.h>
#include <iostream>
#include <assert.h>
extern "C" {
typedef PyObject* (*CreateObjectCallback)();
typedef PyObject* (*NoopCallback)(PyObject* arg);
__declspec(dllexport) PyObject* test(CreateObjectCallback create, NoopCallback noop)
{
// Create the object, with one reference.
PyObject* object = create();
std::cerr << "ref cnt = " << object->ob_refcnt << std::endl;
// Passing object back to Python increments its reference count
// because the parameter of the function is a new reference.
// That python function returns an object (the same one), but
// now you own deleting the reference.
PyObject* object2 = noop(object);
Py_DECREF(object2);
std::cerr << "ref cnt = " << object->ob_refcnt << std::endl;
// Your return the created object, but now that Python knows
// it is a Python object instead of void*, it will decref it.
return object;
}
}
这是我使用的 Python 脚本。您可以将原型(prototype)用作回调函数的装饰器。如果回调需要比传入函数的生命周期更长,这真的很重要。当您像直接使用回调包装器那样调用函数时,回调包装器会在函数返回后被销毁,因为没有更多的引用。
我也改为ctypes.PyDLL
。这不会在调用 C 代码时释放 GIL。由于您正在传递 Python 对象,这似乎是个好主意。
import ctypes
CreateObjectCallback = ctypes.CFUNCTYPE( ctypes.py_object )
NoopCallback = ctypes.CFUNCTYPE( ctypes.py_object, ctypes.py_object )
lib = ctypes.PyDLL('test')
lib.test.restype = ctypes.py_object
lib.test.argtypes = [ CreateObjectCallback, NoopCallback ]
class Foo:
def __del__(self):
print("garbage collect foo");
@CreateObjectCallback
def create():
return Foo()
@NoopCallback
def noop(object):
return object
lib.test(create,noop)
输出:
ref cnt = 1
ref cnt = 1
garbage collect foo
关于Python3 + ctypes回调在简单示例中导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967133/