![pyqt4 funcion lambda python connect qlineedits pyqt4 funcion lambda python connect qlineedits](https://i.ytimg.com/vi/4-nwosP-sDw/maxresdefault.jpg)
And I still don't really know when it's "safe", because I don't know whether there is a later code path which requires to access the dialog's information after closure. So, that is indeed the problem? And so I am required to go through all code looking for exactly the pattern to change it? Does this only apply to nnect(), I only need to look at all connect() calls, or are there other similar I agree, but that requires me to go through code finding all cases where a dialog is shown (e.g. Which confirms exactly what I have spent days discovering and typing in :( Thus, (lambda p: self.someMethod(p)) is very evil :) Beware! As soon as you connect your signal to a lambda slot with a reference to self, your widget will not be garbage-collected! That's because lambda creates a closure with yet another uncollectable reference to the widget. Oh dear! Googling hard, I have just come across, stating: What must I not do if I do not expect such a self-reference to be left around preventing Python/PyQt from actually freeing up the dialog? Note that I do not wish to use tAttribute(QtCore.Qt.WA_DeleteOnClose, True) on all my dialogs (I believe that would solve the leak, but it's not the point). So I need some kind of explanation of what exactly can or cannot be done here, what to look for in code, etc. There is a lot of code with a lot of dialogs with all sorts of code attached. In some shape or form, the fact that we have a lambda which passes self to the outside world must mean Python/PyQt wants to keep a reference to the dialog and this must be preventing its destruction.īut I don't know what to do about it. I can see that at some deep level there must be a reference counting issue here. If I go define (in this case) in the dialog (I actually sub-class from all my QDialogs so I can add stuff) a dedicated function to avoid the lambda: def selfEnsureValidDecimal(self) If the lambda does not need to pass self out as an argument, there will be no leak. The vital bit is: they connect() to a lambda which references self. Some of them have: from elsewhere import ensureValidDecimal
![pyqt4 funcion lambda python connect qlineedits pyqt4 funcion lambda python connect qlineedits](https://satyakide.files.wordpress.com/2020/03/4.-run_win.jpg)
![pyqt4 funcion lambda python connect qlineedits pyqt4 funcion lambda python connect qlineedits](https://coderslegacy.com/wp-content/uploads/2020/06/PyQtQLabel.jpg)
I have finally tracked down the problematic line in the dialog's _init_(). I see an ever-increasing number of these dialogs, one per each time it's constructed and executed, when & only when the code in the dialog is as follows. I discover this by inspecting () and reporting all QDialogs which are still in existence. All I do to test is go into the dialog and immediately close it. That means that every time that code gets executed, yet another new dialog is left around in memory, which adds up over time. The instance of the dialog stays in existence permanently (as long as the caller exists, which for me is till end of program). My definition of "leak" here is: after executing from somewhere else dlg = QDialog(self) I have spent two days commenting in & out bits of code to try to discover why some of its QDialogs "leak" after calling QDialog.exec(). If there are any PyQt users here who can answer, or you're a PyQt user and you'd like to know, please read and/or comment.
![pyqt4 funcion lambda python connect qlineedits pyqt4 funcion lambda python connect qlineedits](https://files.realpython.com/media/grid-layout.3bbf850cab1e.png)
I have posted the following to the PyQt mailing list.