[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 *

    QObject(o1), QObject(o1)
    print sys.getrefcount(o1) , len(o1.children())

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

    #decrease reference count of o1 by one.
    del o1

    #It is destructed!
    print c.isEmpty()


More information about the PyQt mailing list