Short question:
Can signal handlers memory leak.
Long question:
In C#, if I attach a handler to an event
left_object.left_event += right_object.right_handler
Then I need to remove the handler when I get rid of right_object
or the garbage collector will never dispose of it (since left_object.left_event
retains a pointer to right_object
)
Is the same also true of PyQt signals and slots.
left_object.left_signal.connect( right_object.right_handler )
I see from this question that Qt automatically delinks signals and slots, when the destructor of either left_object
or right_object
is called, but in Python I cannot explicitly call the destructor, and right_handler
is a plain-old-function.
Do I need to remove the handler to prevent right_object
s memory-leaking, or does PyQt use some sort of weak-referencing?
While one of the answers touches on this, this similar question asks about how PyQt handles objects inside lambda expressions, not how PyQt handles signals.
It is possible to have a memory leak with this design. You need to disconnect the signals and slots if those connections contain references which are keeping objects alive.
Whether or not this actually happens depends on what exactly right_handler
is. If right_handler
is a closure with a reference to self
then you have this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With