[PyQt] Bug in QTableView.setModel()

Aaron Digulla digulla at hepe.com
Tue Jan 8 16:13:05 GMT 2008

Quoting Hans-Peter Jansen <hpj at urpla.net>:

> First of all, happy new year, Phil & and PyQtnistas!

Thanks, to you, too. :)

>> The difference is that in C++ the effect of
>> the bug is a memory leak, but in PyQt it is often a crash.
> This part is what bugs me, and surely caught every PyQt user in one or
> another way. Is there a technically feasible way to warn a user about
> loosing a python reference of an still existing Qt object, probably
> controllable by a sip function.

This is what made me leave C++ about ten years ago and never come back :-)

Ok, so what can we do? Actually, there is something that we can do. My  
knowledge of C++ is a bit rusty, so the details might be wrong, but  
you'll get the idea: Create a development/debug version of Qt+PyQt.  
This version is just like the original with one exception: QObject has  
an additional field "pyRef". If such an object passes through SIP, put  
the python object in which it is wrapped, into that field.

If the python object is GC'd, set the field to null. In the dtor of  
QObject, check that the field is null. If it isn't, then the QObject  
must still have some link to an existing Python object.

So when I have a memory issue in PyQt, I could install that version  
along with the "normal" one and use PYTHONPATH and  
LIBPATH/LD_LIBRARY_PATH to switch between the two. If you want to add  
icing to the topping, save a stack trace in an additional field when  
it is set for the first time. Then you can even tell where the object  
was first referenced.

Another wa would be to save all objects which pass through SIP in map  
(QObject -> Python wrapper), connect to the destroyed(QObject) signal  
and check the state when it is emitted. Does SIP notice when the  
python wrapper is destroyed? If not, it's probably possible to patch  
the GC code of Python to call an additional routine for the wrapper  

Or it might even be possible check when the automatic disconnect  
happens in the dtor (not sure if that happens on the sender or  
receiver, though).

Remember: Nothing is impossible. Sometimes, it just takes a while.


Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."

More information about the PyQt mailing list