在使用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