[PySide] PyQt. How to shoot yourself in the foot

anatoly techtonik techtonik at gmail.com
Mon Aug 25 10:15:01 CEST 2014


On Mon, Aug 25, 2014 at 9:11 AM, Andrei Kopats <hlamer at tut.by> wrote:
> Hi
>
> I published an article about memory management in PyQt and PySide. I think
> it might be interesting for somebody in this list.
>
> http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html

Very interesting stuff actually. You may want to fix indentation in the first
snippet.

About Python not calling constructor of QObject - are there cases
when you don't need to call QObject constructor? If not, it is should
be possible
to either:
  a. fix binding
  b. provide an automatic import through __new__/metaclass mechanizm
BTW, it is possible to detect 'disconnected' object without catching exception?

"References to all created objects must be saved even if you are not going to
use them." - I can't say that this is a bad design. Knowing that PyQt objects
have scope and realizing the boundaries of this scope is a good practice for the
brain to think about program architecture. I know some serious games where
garbage collector is turned off completely. I guess it is very easy to spot
memory leaks in such case.

> C++ object is deleted by Qt but Python wrapper still exists
...
> A slot of the deleted object is called by timer or signal from the external world

So, the correct strategy is to disconnect all signals and reset timers when an
object is destroyed. I fail to see how C++ -- Python link play major
role in this architecture. Even if you implement decoupled system with signals
and timers in pure Python, it will still be flaky when referenced objects are
destroyed. So how Qt propose to handle this on pure C++ level?

> C++ method overridden by Python wrapper

Crashes. Looks like a missing part in PySide. If it is fixable, it needs a bug
report to keep track of.

Few more questions (I don't have CS degree, so they may be lame):
> When deleting an object which has Python-slots, disconnect the slots manually

How to distinguish object with Python-slots from, well, object with standard
PySide slots?

> To be notified about an object deletion use the QObject.destroyed signal but not
> the __del__ method of a Python wrapper

How to be notified with a __del__ method?

> Don't use QTimer.singleShot for an object which might be deleted. It is impossible
> to stop and disconnect such a timer

Not clear why singleShot, being connected to an object method, doesn't keep a
reference to this object.

> Don't use lambda function as a slot. It is impossible to disconnect it.

Also interesting to know the gory details - why? If signals can reach
it, then in theory a disconnector could reach it too.

P.S. gdb really helps to debug application crashes - I think it works even
       on Windows, see the cheatsheet
       https://code.google.com/p/spyderlib/wiki/HowToDebugQtCrash
-- 
anatoly t.



More information about the PySide mailing list