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

Erik Janssens Erik.Janssens at conceptive.be
Thu Mar 29 12:36:43 BST 2012


creating/deleting children does not change the refcount
of a QObject, since this is done purely in C++

On Thu, 2012-03-29 at 19:22 +0800, Goldfish Huang wrote:
> 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/
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt




More information about the PyQt mailing list