Python3+ctypes回调导致简单examp中的内存泄漏

2024-06-18 11:18:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用Python 3代码和C++代码使用CyType的复杂程序上工作时,我发现内存泄漏可以很容易地被下面的示例所复制。在

< >我的C++代码使用回调函数创建Python对象。接下来,它调用Python对象的另一个回调函数,它只返回它的参数。第二个回调会导致对象的引用计数增加。因此,对象永远不会被垃圾回收。在

这是Python代码(文件虫子.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++代码(文件)错误.cpp)公司名称:

^{pr2}$

下面是我用来编译和运行的命令:

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


Tags: 对象函数代码pyrefobjectfoolib
1条回答
网友
1楼 · 发布于 2024-06-18 11:18:14

你在传递Python对象。其中一个对象被传递到C代码中,而不是被传递出去,因此负责该引用计数。以下是一些有效的方法,但是我已经将void*改为{},因为它们是这样的:

#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对象,这似乎是个好主意。在

^{pr2}$

输出:

ref cnt = 1
ref cnt = 1
garbage collect foo

相关问题 更多 >