[PyQt] Reference leak in QListView.setModel?
Phil Thompson
phil at riverbankcomputing.com
Wed Sep 10 23:27:44 BST 2008
On Wed, 10 Sep 2008 16:59:04 +0000 (UTC), dfragi <daelfin at gmail.com> wrote:
> Hello,
> While trying to get rid of a memory leak in my program, I
> tracked it down to this:
>
> ##########################################################
> #
> # import sys, gc
> # from PyQt4.Qt import *
> #
> # class MyModel(QAbstractListModel):
> # def rowCount(self, parent):
> # return 5
> #
> # def data(self, index, role):
> # if role == Qt.DisplayRole:
> # return QVariant(str(index.row()))
> # else:
> # return QVariant()
> #
> # def main():
> # app = QApplication(sys.argv)
> #
> # model = MyModel()
> #
> # lv = QListView()
> # print gc.get_referrers(model)
> # lv.setModel(model)
> # print gc.get_referrers(model)
> # QApplication.processEvents()
> # print gc.get_referrers(model)
> # lv.setModel(None)
> # print gc.get_referrers(model)
> # lv.show()
> #
> # app.exec_()
> #
> # if __name__ == '__main__':
> # main()
> ##########################################################
>
> This outputs the following:
>
> [<frame object at 0x8c7bd0>]
> [<frame object at 0x8c7bd0>]
> [<frame object at 0x8c7bd0>,
> <bound method MyModel.rowCount of <__main__.MyModel object at
0xd8f1e0>>,
> <bound method MyModel.data of <__main__.MyModel object at 0xd8f1e0>>]
> [<frame object at 0x8c7bd0>,
> <bound method MyModel.rowCount of <__main__.MyModel object at
0xd8f1e0>>,
> <bound method MyModel.data of <__main__.MyModel object at 0xd8f1e0>>]
>
> i.e. after setModel(), the model isn't referenced anywhere else other
> than the current frame (the local name 'model'), which is ok. But after
> QApplication.processEvents, references to the bound methods rowCount()
> and data() are being kept by PyQt, and not released even after the
> model is released. This keeps the model from being garbage-collected.
>
> Is this a bug or should I be doing something differently?
This needs thinking about. Which means don't expect answer soon, but I
won't forget about it.
Phil
More information about the PyQt
mailing list