我不能在删除对象时调用__dealloc__ [英] I can't get __dealloc__ be called when deleting an object

查看:203
本文介绍了我不能在删除对象时调用__dealloc__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C ++类:

.H

class ALabSet: public LabSet {
public:
    PyObject *m_obj;

    ALabSet(PyObject *obj);

    virtual ~ALabSet();

    PyObject *GetPyObj();

};

.CPP

ALabSet::ALabSet(PyObject *obj): LabSet() {

    this->m_obj = obj;
    // Provided by "cyelp_api.h"
    if (import_cyelp()) {
    } else {
        Py_XINCREF(this->m_obj);
    }

}


ALabSet::~ALabSet() {
    Py_XDECREF(this->m_obj);
}


PyObject *ALabSet::GetPyObj() {
    return this->m_obj;
}

我使用Cython将其暴露如下:

I exposed it as follows with Cython :

cdef extern from "adapter/ALabSiteSetsManager.h" namespace "elps" :
    cdef cppclass ALabSet:
        ALabSet(PyObject *)

        PyObject *GetPyObj()



cdef class PyLabSet:
    cdef ALabSet *thisptr

    def __cinit__(self):
       self.thisptr = new ALabSet(<PyObject *>self)

    def __dealloc__(self):
       print "delete from PY !"
       if self.thisptr:
           del self.thisptr

我的问题是我不知道如何从Python调用析构函数。下面没有什么:

My problem is that I can't figure out how to get the destructor called from Python. The following does exactly nothing :

a_set = PyLabSet()
del a_set

我在网络上找不到类似的问题。您是否有任何想法是在这里加入我的?

I can't find similar issues on the web. Does any of you has an idea of is appening to me here ?

我缺少有关参考计数管理的内容,或...

I'm I missing something about reference counting management, or ...

非常感谢

推荐答案

del a_set 删除对对象(本地变量)的引用。在C ++对象中还有另一个引用。这被称为参考周期。循环GC 可以在一段时间后收集此消息。但是,无法保证这种情况何时发生(甚至是否),因此您不应该依赖 1 。例如,包含纯Python对象的参考周期与 __del __ 特殊方法被记录为不能完全释放 。我不知道Cython的实现 __ dealloc __ 是否触发这种行为,但是如前所述,销毁是不确定的。如果你想释放一些资源(例如一个内存块不是一个Python对象,一个文件,一个连接,一个锁等),你应该暴露一个明确的方式这样做手动(参见 close 各种对象的方法)。上下文管理器可以简化执行此操作的客户端代码。

del a_set removes a reference to the object (the local variable). There's still another reference, in the C++ object. This is known as a reference cycle. The cycle GC could collect this after a while. However, there is no guarantee when (or even if) this happens, so you should not rely on it1. For example, reference cycles containing pure Python objects with a __del__ special method are documented to not be freed at all. I don't know if Cython's implementation of __dealloc__ triggers this behavior, but as outlined before, destruction isn't deterministic anyway. If you want to free some resource (e.g. a block of memory that isn't a Python object, a file, a connection, a lock, etc.) you should expose an explicit way of do so manually (cf. the close methods of various objects). Context managers can simplify client code doing this.

免责声明:几乎所有这些都是特定于CPython的。

Disclaimer: Almost all of this is CPython-specific.

1 有些人更喜欢将GC视为模拟无限内存可用性的抽象,而不是破坏无法访问的对象。使用这种方法,变得非常明显,销毁不是确定性的,甚至不能保证。

1 Some people prefer thinking of GC as an abstraction that simulates availability of infinite memory, rather than something that destroying unreachable objects. With this approach, it becomes quite obvious that destruction is not deterministic and not even guaranteed.

这篇关于我不能在删除对象时调用__dealloc__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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