Re: [PyQt] memory leak on a Mac
I don't have those problems. How did you install PyQt? I use MacPorts (using Python 2.7). Arne, thanks for the MacPorts tip. I have installed pyqt through MacPorts and it works just fine. No memory leak anymore. After figuring out how to get through our firewall and making the MacPorts python the current python, it worked great. D ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] memory leak on a Mac
Am 27.01.2011 um 18:12 schrieb Danny Shevitz: Any PyQt application generates the same message. If I run something from the samples directory, I get the same memory leak, so it is nothing in my code. I don't have those problems. How did you install PyQt? I use MacPorts (using Python 2.7). Regards, Arne -- Dipl.-Inform. Arne Schmitz Phone +49 (0)241 80-21817 Computer Graphics Group Mobile +49 (0)151 29145947 RWTH Aachen University Fax +49 (0)241 80-22899 Ahornstrasse 55, 52074 Aachen, Germany http://www.rwth-graphics.de ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] memory leak on a Mac
Arne Schmitz arne.schmitz at gmx.net writes: Am 27.01.2011 um 18:12 schrieb Danny Shevitz: Any PyQt application generates the same message. If I run something from the samples directory, I get the same memory leak, so it is nothing in my code. I don't have those problems. How did you install PyQt? I use MacPorts (using Python 2.7). Regards, Arne I got the gzipped tarball, unpacked it. cd'ed to the directory. Then I ran: python configure.py make make install. I had to do this for sip as well. D ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] memory leak using QMainWindow.removeToolBar
On Mon, 16 Aug 2010 22:59:22 + (UTC), danny shev...@lanl.gov wrote: thanks, your solution works, and stops my code from seg faulting. I'm curious why mine doesn't work. In the documentation for QMainWindow http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmainwindow.html#addToolBar-3 The third overloading of the addToolBar method, was what I was using. It should be equivalent, but I must be missing something. There does seem to be a bug. Even though addToolBar() takes ownership of the toolbar, removeToolBar() doesn't change the current ownership. It will be fixed in tonight's snapshot. The consequence is that you have to do extra work to really delete a removed toolbar, ie. reparent it to None. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] memory leak using QMainWindow.removeToolBar
On 16/08/10 18:27, danny wrote: Howdy, I have run into this curious problem that I think is caused by a memory leak or improper reference counting related to toolbars in QMainWindow.removeToolBar. The problem appears when you remove a toolbar but keep an instance variable maintaining a reference to the toolbar. I am aware that removeToolBar only hides the toolbar and does not delete it. I also am aware that the following code may seem foolish, but it does show the bug. The code below is a minimal example demonstrating the problem. It is 34 lines long. Simply run the app, and close via the close box. The app will not close gracefully, but rather seg fault. This has been validated on windows and Linux. I don't believe the following code is doing anything stupid enough to justify a seg fault. The problem disappears is you remove the instance reference to the toolbar (self.editToolBar = editToolBar). Notice that the code is intended to reside within an SDI framework app like Mark Summerfield uses in his excellent PyQT book. The instances variables probably keep additional references around that are part of the problem. Explicitly removing the attribute, gets rid of the problem. Included below is the source code. Please let me know if this is a bug or my own stupidity. I am running python 2.5.1, pyqt 4.7.3 thanks, Danny #% import sys from PyQt4 import QtGui, QtCore, QtSql def isAlive(qobj): import sip try: sip.unwrapinstance(qobj) except RuntimeError: return False return True class MyApp(QtGui.QMainWindow): NextId = 1 Instances = set() def __init__(self, parent=None): super(MyApp, self).__init__(parent) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) MyApp.Instances.add(self) self.editToolBar = self.addToolBar(Edit) self.removeToolBar(self.editToolBar) # delattr(self, 'editToolBar') # this gets rid of the seg fault self.destroyed.connect(MyApp.updateInstances) self.setCentralWidget(QtGui.QWidget()) @staticmethod def updateInstances(qobj): MyApp.Instances = set([window for window \ in MyApp.Instances if isAlive(window)]) if __name__=='__main__': application = QtGui.QApplication(sys.argv) MyApp().show() application.exec_() i don't think it is a bug. the real problem is with how the toolbar is being created, not with how it's being destroyed. try something like this: import sys from PyQt4 import QtGui, QtCore class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self._toolbar = QtGui.QToolBar('Edit', self) self.addToolBar(self._toolbar) self.removeToolBar(self._toolbar) if __name__=='__main__': app = QtGui.QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_()) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] memory leak using QMainWindow.removeToolBar
thanks, your solution works, and stops my code from seg faulting. I'm curious why mine doesn't work. In the documentation for QMainWindow http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmainwindow.html#addToolBar-3 The third overloading of the addToolBar method, was what I was using. It should be equivalent, but I must be missing something. thanks, D ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
On Sat, 2008-07-05 at 23:04 +0100, Phil Thompson wrote: On Thu, 03 Jul 2008 16:04:42 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: Hi Phil, with SIP 4.7.6, PyQt 4.2.2, Qt 4.4.0: import sip import weakref from PyQt4.Qt import * class MyWidget(QWidget): def sizeHint(self): return QSize(900, 700) app = QApplication([]) ws = MyWidget(None) wr = weakref.ref(ws) L = QVBoxLayout(None) L.addWidget(ws) L.activate() del L del ws import gc gc.collect() assert wr() is None The assert triggers, meaning that the object of type MyWidget is not released. This appears to be a Qt problem. Although the docs say that a layout takes ownership of the widget when addWidget() is called it leaves the destruction of the widget to the eventual owner of the layout and doesn't call the widget's dtor itself. If the layout is never used (ie. never passed as an argument to QWidget.setLayout()) then all the widgets in the layout will leak. An equivalent C++ version behaves in the same way. I could change addWidget() so that the layout doesn't take ownership of the widget (ie. to match the implementation rather than the documentation) but that will break any code that creates a populated layout and returns it from a function. I believe that it's more correct if you fix this code not to have any memory leak. Either that, or you get smarter wrt when the ownership is transferred from Python to C++ (that is, when the layout is reparented to a widget, if ever). And in any case, you should probably raise this issue with Trolltech (it's at least a documentation issue, I'd say). Anyway, it turns out that I had reduced the testcase a little too much: it was not the actual memory leak I was seeing in my application. The correct testcase is this one: #!/usr/bin/env python import sip import weakref from PyQt4.Qt import * class MyWidget(QWidget): def sizeHint(self): return QSize(900, 700) app = QApplication([]) ww = QWidget(None) ws = MyWidget(None) wr = weakref.ref(ws) L = QVBoxLayout(ww) L.addWidget(ws) L.activate() del L del ws del ww import gc gc.collect() assert wr() is None This memory leak is related to the sizeHint() method (if you comment it, there's no leak anymore) and to the fact that it is actually called at least once (if you comment the .activate() call, there's no leak anymore). -- Giovanni Bajo Develer S.r.l. http://www.develer.com ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
On Sun, 06 Jul 2008 15:18:10 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: On Sat, 2008-07-05 at 23:04 +0100, Phil Thompson wrote: On Thu, 03 Jul 2008 16:04:42 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: Hi Phil, with SIP 4.7.6, PyQt 4.2.2, Qt 4.4.0: import sip import weakref from PyQt4.Qt import * class MyWidget(QWidget): def sizeHint(self): return QSize(900, 700) app = QApplication([]) ws = MyWidget(None) wr = weakref.ref(ws) L = QVBoxLayout(None) L.addWidget(ws) L.activate() del L del ws import gc gc.collect() assert wr() is None The assert triggers, meaning that the object of type MyWidget is not released. This appears to be a Qt problem. Although the docs say that a layout takes ownership of the widget when addWidget() is called it leaves the destruction of the widget to the eventual owner of the layout and doesn't call the widget's dtor itself. If the layout is never used (ie. never passed as an argument to QWidget.setLayout()) then all the widgets in the layout will leak. An equivalent C++ version behaves in the same way. I could change addWidget() so that the layout doesn't take ownership of the widget (ie. to match the implementation rather than the documentation) but that will break any code that creates a populated layout and returns it from a function. I believe that it's more correct if you fix this code not to have any memory leak. I disagree, I don't work around Qt bugs in PyQt. Either that, or you get smarter wrt when the ownership is transferred from Python to C++ (that is, when the layout is reparented to a widget, if ever). The reason I'm not going to do this is that it would break lots of code (at least, lots of my code). And in any case, you should probably raise this issue with Trolltech (it's at least a documentation issue, I'd say). Agreed. Anyway, it turns out that I had reduced the testcase a little too much: it was not the actual memory leak I was seeing in my application. The correct testcase is this one: Fixed in tonight's SIP snapshot. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
On Sun, 2008-07-06 at 17:29 +0100, Phil Thompson wrote: On Sun, 06 Jul 2008 15:18:10 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: On Sat, 2008-07-05 at 23:04 +0100, Phil Thompson wrote: On Thu, 03 Jul 2008 16:04:42 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: Hi Phil, with SIP 4.7.6, PyQt 4.2.2, Qt 4.4.0: import sip import weakref from PyQt4.Qt import * class MyWidget(QWidget): def sizeHint(self): return QSize(900, 700) app = QApplication([]) ws = MyWidget(None) wr = weakref.ref(ws) L = QVBoxLayout(None) L.addWidget(ws) L.activate() del L del ws import gc gc.collect() assert wr() is None The assert triggers, meaning that the object of type MyWidget is not released. This appears to be a Qt problem. Although the docs say that a layout takes ownership of the widget when addWidget() is called it leaves the destruction of the widget to the eventual owner of the layout and doesn't call the widget's dtor itself. If the layout is never used (ie. never passed as an argument to QWidget.setLayout()) then all the widgets in the layout will leak. An equivalent C++ version behaves in the same way. I could change addWidget() so that the layout doesn't take ownership of the widget (ie. to match the implementation rather than the documentation) but that will break any code that creates a populated layout and returns it from a function. I believe that it's more correct if you fix this code not to have any memory leak. I disagree, I don't work around Qt bugs in PyQt. Depends on whether you consider it a bug in the Qt documentation or a bug in the Qt implementation. If it's the former, probably something should be changed in PyQt as well. If it's the latter, then yes, leave it to the Trolltech guys. Either that, or you get smarter wrt when the ownership is transferred from Python to C++ (that is, when the layout is reparented to a widget, if ever). The reason I'm not going to do this is that it would break lots of code (at least, lots of my code). You can wait a major version to apply this breaker, but it's still worthwhile. I think that explicitly breaking code is better than implicitly causing memory leaks which hard to find and debug. Anyway, it turns out that I had reduced the testcase a little too much: it was not the actual memory leak I was seeing in my application. The correct testcase is this one: Fixed in tonight's SIP snapshot. Many thanks! -- Giovanni Bajo Develer S.r.l. http://www.develer.com ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
On Thu, 03 Jul 2008 16:04:42 +0200, Giovanni Bajo [EMAIL PROTECTED] wrote: Hi Phil, with SIP 4.7.6, PyQt 4.2.2, Qt 4.4.0: import sip import weakref from PyQt4.Qt import * class MyWidget(QWidget): def sizeHint(self): return QSize(900, 700) app = QApplication([]) ws = MyWidget(None) wr = weakref.ref(ws) L = QVBoxLayout(None) L.addWidget(ws) L.activate() del L del ws import gc gc.collect() assert wr() is None The assert triggers, meaning that the object of type MyWidget is not released. This appears to be a Qt problem. Although the docs say that a layout takes ownership of the widget when addWidget() is called it leaves the destruction of the widget to the eventual owner of the layout and doesn't call the widget's dtor itself. If the layout is never used (ie. never passed as an argument to QWidget.setLayout()) then all the widgets in the layout will leak. An equivalent C++ version behaves in the same way. I could change addWidget() so that the layout doesn't take ownership of the widget (ie. to match the implementation rather than the documentation) but that will break any code that creates a populated layout and returns it from a function. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
Giovanni Bajo wrote: The assert triggers, meaning that the object of type MyWidget is not released. Hi Giovanni, Yes, this is a known bug: SIP keeps a hard reference to bound methods in its method cache (the bound method being ws.sizeHint in your example). Since bound methods keep a reference to the object they're bound to, your object is never freed and its memory is leaked. I understand Phil is already on it. -- S. ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak
On Fri, 2008-07-04 at 10:48 +0200, Sundance wrote: Giovanni Bajo wrote: The assert triggers, meaning that the object of type MyWidget is not released. Hi Giovanni, Yes, this is a known bug: SIP keeps a hard reference to bound methods in its method cache (the bound method being ws.sizeHint in your example). Since bound methods keep a reference to the object they're bound to, your object is never freed and its memory is leaked. I understand Phil is already on it. It's a rather new regression, though. It wasn't happening 6 months ago. -- Giovanni Bajo Develer S.r.l. http://www.develer.com ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
Hurray! Thanks to Justin Noel on the qt-interest list, the following code now works and doesn't leak! Noam === #!/usr/bin/env python import sys from PyQt4 import QtCore, QtGui from PyQt4.QtCore import Qt class TableModel(QtCore.QAbstractTableModel): def rowCount(self, index): return 1000 if not index.isValid() else 0 def columnCount(self, index): return 1000 if not index.isValid() else 0 def data(self, index, role): if index.isValid() and role == Qt.DisplayRole: return QtCore.QVariant(str((index.row(), index.column( else: return QtCore.QVariant() def headerData(self, col, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QtCore.QVariant(str(col)) if orientation == Qt.Vertical and role == Qt.DisplayRole: return QtCore.QVariant(str(col)) return QtCore.QVariant() def main(): app = QtGui.QApplication(sys.argv) model = TableModel() tableView = QtGui.QTableView() tableView.setModel(model) tableView.show() sys.exit(app.exec_()) if __name__ == '__main__': main() ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
2007/12/23, Phil Thompson [EMAIL PROTECTED]: Is there anything that can be done? Not if I can't reproduce the problem. I'm using current SIP and PyQt snapshots and Qt 4.3.3. It also works for me on windows, using the latest binary. But have you tried it on linux? I'm using ubuntu 7.10, which uses gcc 4.1.3. Noam ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
On 23.12.07 13:59:30, Noam Raphael wrote: 2007/12/23, Phil Thompson [EMAIL PROTECTED]: Is there anything that can be done? Not if I can't reproduce the problem. I'm using current SIP and PyQt snapshots and Qt 4.3.3. It also works for me on windows, using the latest binary. But have you tried it on linux? I'm using ubuntu 7.10, which uses gcc 4.1.3. Works fine here on Debian unstable, with a bit older PyQt4(I think from November) and Qt4.3.2. Yes the memory usage increases when starting to scroll, but not that much. After scrolling from one end to the other on both bars the python process is still under 30MB of res-memory according to top. Andreas -- Time to be aggressive. Go after a tattooed Virgo. ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
On Sunday 23 December 2007, Noam Raphael wrote: 2007/12/23, Phil Thompson [EMAIL PROTECTED]: Is there anything that can be done? Not if I can't reproduce the problem. I'm using current SIP and PyQt snapshots and Qt 4.3.3. It also works for me on windows, using the latest binary. But have you tried it on linux? Yes. I'm using ubuntu 7.10, which uses gcc 4.1.3. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
2007/12/23, Andreas Pakulat [EMAIL PROTECTED]: Works fine here on Debian unstable, with a bit older PyQt4(I think from November) and Qt4.3.2. Yes the memory usage increases when starting to scroll, but not that much. After scrolling from one end to the other on both bars the python process is still under 30MB of res-memory according to top. Andreas If you enlarge the window and scroll slowly, does it increase any more? I think that every displayed cell leaks something, so if you display more cells, you leak more memory. Noam ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
On Sunday 23 December 2007, Noam Raphael wrote: 2007/12/23, Andreas Pakulat [EMAIL PROTECTED]: Works fine here on Debian unstable, with a bit older PyQt4(I think from November) and Qt4.3.2. Yes the memory usage increases when starting to scroll, but not that much. After scrolling from one end to the other on both bars the python process is still under 30MB of res-memory according to top. Andreas If you enlarge the window and scroll slowly, does it increase any more? I think that every displayed cell leaks something, so if you display more cells, you leak more memory. I see that behaviour, but a C++ version behaves in the same way so it's not a PyQt problem. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
2007/12/21, Phil Thompson [EMAIL PROTECTED]: Works fine for me with current versions. Phil Ok, I now compiled by myself PyQt 4.3.3 with sip 4.7.3. I have qt 4.3.2 installed. The memory still leaks. Is there anything that can be done? Noam ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
On Saturday 22 December 2007, Noam Raphael wrote: 2007/12/21, Phil Thompson [EMAIL PROTECTED]: Works fine for me with current versions. Phil Ok, I now compiled by myself PyQt 4.3.3 with sip 4.7.3. I have qt 4.3.2 installed. The memory still leaks. Is there anything that can be done? Not if I can't reproduce the problem. I'm using current SIP and PyQt snapshots and Qt 4.3.3. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Memory leak when using QAbstractTableModel
On Thursday 20 December 2007, Noam Raphael wrote: Hello, I created a simple program which uses QAbstractTableModel to display a table with 1000x1000 cells. It works, but if I enlarge the window and scroll a little bit the memory consumption of the program jumps to the skies (If I weren't careful, my system would have stuck.) Here's the program. I'm using PyQt 4.3-2ubuntu7 on Ubuntu 7.10. == #!/usr/bin/env python import sys from PyQt4 import QtCore, QtGui from PyQt4.QtCore import Qt class TableModel(QtCore.QAbstractTableModel): def rowCount(self, index): return 1000 def columnCount(self, index): return 1000 def data(self, index, role): return QtCore.QVariant(str((index.row(), index.column( def headerData(self, col, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QtCore.QVariant(str(col)) if orientation == Qt.Vertical and role == Qt.DisplayRole: return QtCore.QVariant(str(col)) return QtCore.QVariant() def main(): app = QtGui.QApplication(sys.argv) model = TableModel() tableView = QtGui.QTableView() tableView.setModel(model) tableView.show() sys.exit(app.exec_()) if __name__ == '__main__': main() == Is there some way I can solve this? Works fine for me with current versions. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt