Python C模块内存

2024-05-16 02:53:44 发布

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

我有一个适用于python的C库,它是rdpy(https://github.com/citronneur/rdpy)项目的一部分。 但是,有一个内存泄漏,我无法在python代码中消除它:删除对对象的引用(input=None,output=None),gc.收集()等。泄漏分析器(如pympler)也不会显示占用内存的对象。我还发现在rdesktop项目(https://github.com/rdesktop/rdesktop/blob/master/bitmap.c)中使用了相同的代码,但在那里它工作正常,可能是与python集成的问题。这个函数中的什么可以使内存泄漏?你知道吗

/* Specific rename for RDPY integration */
#define uint8   unsigned char
#define uint16  unsigned short
#define unimpl(str, code)

#define  RD_BOOL    int
#define False   0
#define True    1
/* end specific rename */

......................................

static PyObject*
bitmap_decompress_wrapper(PyObject* self, PyObject* args)
{
    Py_buffer output, input;
    int width = 0, height = 0, bpp = 0;

    if (!PyArg_ParseTuple(args, "s*iis*i", &output, &width, &height, &input, &bpp))
        return NULL;

    if(bitmap_decompress((uint8*)output.buf, width, height, (uint8*)input.buf, input.len, bpp) == False)
        return NULL;

    Py_RETURN_NONE;
}

static PyMethodDef rle_methods[] =
{
     {"bitmap_decompress", bitmap_decompress_wrapper, METH_VARARGS, "decompress bitmap from microsoft rle algorithm."},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initrle(void)
{
     (void) Py_InitModule("rle", rle_methods);
}

Tags: 内存pyinputoutputwidthdecompressnullpyobject