在简单的示例中,Python3 + ctypes回调导致内存泄漏

时间:2018-10-24 10:51:42

标签: python c++ memory-leaks garbage-collection ctypes

在使用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中的错误引起的吗?

1 个答案:

答案 0 :(得分:1)

您正在传递Python对象。您的对象之一将传递到您的C代码中,而不是传递出去,因此 you 负责该引用计数。这是可行的方法,但是我将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脚本。您可以将原型用作回调函数的装饰器。如果回调的寿命比传递给它的函数的寿命长,这确实很重要。当您像直接使用回调包装器一样调用该函数时,由于没有更多引用,因此在函数返回后将破坏回调包装器。

我也更改为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