Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can signal handlers memory leak in PyQt? [duplicate]

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_objects 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.

like image 854
c z Avatar asked May 12 '17 11:05

c z


1 Answers

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.

like image 101
spraff Avatar answered Sep 21 '22 03:09

spraff