[PyQt] How SIP detects that a QObject need to destruct.

Goldfish Huang hgoldfish at gmail.com
Thu Mar 29 12:22:10 BST 2012


Hi, all.

I am doubted how  SIP deal with QObject's cycle reference, when I try
to fix some unknown memory leaks. It seems that SIP only keep a
reference to QObject, whatever how many children it has. Then a
QObject must have not less than 2 reference count, one from Python and
another from SIP. The question is, when I delete the QObject from
Python, how SIP detects that and decrease the reference? It supposed
that the reference count will be decreased to 1, and never destructed.

The code pastes below:

    import sys, gc
    from PyQt4.QtCore import *

    gc.disable()
    c=QObjectCleanupHandler()
    o1=QObject()
    QObject(o1), QObject(o1)
    c.add(o1)
    print sys.getrefcount(o1) , len(o1.children())

    #delete a child but the reference count remains 2.
    o1.children()[0].setParent(None)
    print sys.getrefcount(o1) , len(o1.children())

    #decrease reference count of o1 by one.
    del o1

    #It is destructed!
    print c.isEmpty()


--
免服务器办公室聊天软件、笔记本、日记本:http://besteam.im/。
Python及Qt相关Blog:http://besteam.im/blogs/


More information about the PyQt mailing list