删除Python对象时,Ctypes Structures和POINTERS是否自动释放内存? [英] Does Ctypes Structures and POINTERS automatically free the memory when the Python object is deleted?

查看:141
本文介绍了删除Python对象时,Ctypes Structures和POINTERS是否自动释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python CType时,有一些Structures可以让您在Python端克隆c结构,而POINTERS对象可以根据内存地址值创建复杂的Python对象,并且可以用来通过引用传回对象

When using Python CTypes there are the Structures, that allow you to clone c-structures on the Python side, and the POINTERS objects that create a sofisticated Python Object from a memory address value and can be used to pass objects by reference back and forth C code.

在文档或其他地方找不到的是当Python对象包含从返回指针中取消引用的Structure类时发生的情况从C代码中删除(即-该结构的C函数分配的内存)本身被删除。原始C结构的内存是否已释放?如果不是怎么做呢?

What I could not find on the documentation or elsewhere is what happens when a Python object containing a Structure class that was de-referenced from a returning pointer from C Code (that is - the C function alocated memory for the structure) is itself deleted. Is the memory for the original C structure freed? If not how to do it?

此外-如果结构包含指针本身,也指向C函数也分配的其他数据,该怎么办?删除Structure对象是否释放其成员上的Pointer? (我对此很怀疑)--怎么做?尝试从Python中调用系统免费以获取结构中的指针对我来说是Python崩溃。

Furthermore -- what if the Structure contains Pointers itself, to other data that was also allocated by the C function? Does the deletion of the Structure object frees the Pointers onits members? (I doubt so) Else - -how to do it? Trying to call the system "free" from Python for the Pointers in the Structure is crashing Python for me.

换句话说,我已经用ac Function填充了此结构调用:

In other words, I have this structure filled up by a c Function call:

class PIX(ctypes.Structure):
    """Comments not generated
    """
    _fields_ = [
        ("w", ctypes.c_uint32),
        ("h", ctypes.c_uint32),
        ("d", ctypes.c_uint32),
        ("wpl", ctypes.c_uint32),
        ("refcount", ctypes.c_uint32),
        ("xres", ctypes.c_uint32),
        ("yres", ctypes.c_uint32),
        ("informat", ctypes.c_int32),
        ("text", ctypes.POINTER(ctypes.c_char)),
        ("colormap", ctypes.POINTER(PIXCOLORMAP)),
        ("data", ctypes.POINTER(ctypes.c_uint32))
    ]

我想从Python代码中释放正在使用的内存。

And I want to free the memory it is using up from Python code.

推荐答案

内存无法释放,因为Python没有知道是否或如何释放它。比较这两个函数:

The memory is not freed, because Python has no idea if or how it should be freed. Compare these two functions:

void testfunc1(PIX *pix)
{
    static char staticBuffer[256] = "static memory";
    pix->text = staticBuffer;
}

void testfunc2(PIX *pix)
{
    pix->text = (char *)malloc(32);
    strcpy(pix->text, "dynamic memory");
}

使用如下:

pix1, pix2 = PIX(), PIX()
mylib.testfunc1(ctypes.byref(pix1))
mylib.testfunc2(ctypes.byref(pix2))

然后在某个时候, pix1 pix2 超出范围。发生这种情况时,内部指针什么都不会发生—如果内部指针指向动态内存(如 pix2 not pix1 ),则您有责任释放它。

And then at some point, pix1 and pix2 go out of scope. When that happens, nothing happens to the inner pointers—if they pointed to dynamic memory (as is the case here with pix2 but not pix1), you are responsible for freeing it.

解决此问题的正确方法是,如果在您的C代码中,应提供一个相应的函数来释放该内存。例如:

The proper way to solve this problem is, if you allocate dynamic memory in your C code, you should provide a corresponding function that deallocates that memory. For example:

void freepix(PIX *pix)
{
    free(pix->text);
}


pix2 = PIX()
mylib.testfunc2(ctypes.byref(pix2))
...
mylib.freepix(ctypes.byref(pix2))

这篇关于删除Python对象时,Ctypes Structures和POINTERS是否自动释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆