[PySide] QObject.destroyed() is not emitted
Alexey Vihorev
vihorev at gmail.com
Wed Nov 28 21:24:55 CET 2012
Hi!
Well, I understand that if what I need to do is achievable with explicit
deletion, I should go for it :) The problem is that in my case the deletion
of the object is triggered by deletion of another object - its parent. But I
can't catch the deletion event - neither of the object nor its parent.
That's the problem. The whole reason I started looking into that is that one
of my objects needed to free some external resources when destroyed.
-----Original Message-----
From: Stephan Deibel [mailto:sdeibel at wingware.com]
Sent: 28 ноября 2012 г. 16:33
To: Alexey Vihorev
Cc: pyside at qt-project.org
Subject: Re: [PySide] QObject.destroyed() is not emitted
Alexey Vihorev wrote:
>
> Got problems with the following code:
>
> from PySide import QtCore
>
> def onDestroy(*args):
>
> print('destroyed')
>
> obj = QtCore.QObject()
>
> obj.destroyed.connect(onDestroy)
>
> The onDestroy() method is not called, unless del(obj) is called
> explicitly. In PyQt4 it is called without del(obj). Is that a bug or
> intended behavior? I'm using Qt 4.8.2, PySide 1.1.2 32bit, Windows
> 7-64
>
Since no one responded I'll take a stab at this:
I suspect the life cycle of the QObject under PySide is a little different
and the instance will be deleted later during garbage collection and not
immediately when it goes out of scope. I'm actually not sure why that is
happening in PyQt (seems slightly surprising).
In general if you want to make sure an instance is deleted at a particular
moment in time, you need to delete it explicitly. By that, I mean calling
some sort of delete method (in PySide I think it's
shiboken.delete(obj) and not just "del obj". The latter just deletes your
reference to the instance but doesn't destroy the instance until all other
references are gone and it is garbage collected. Of course calling
shiboken.delete(obj) will be a problem if you still have other references
and try to use them!
There is also obj.deleteLater() which marks an object for deletion but it's
not deleted until the event loop is reached again. Depending on what you're
doing this may be a safer way to delete it.
At first I thought this might be a refcount bug in PySide. However, I'm
thinking not since "del obj" just deletes your reference to it and if there
were extra references hanging around (either on purpose or as a result of a
refcount bug) then it would not be deleting the instance at all even after
"del obj".
I don't understand PySide internals that well so it's possible I'm missing
some subtlety here. I'm going more on my knowledge of Python in general.
- Stephan
More information about the PySide
mailing list