Memory leak in QHeaderView with hidden sections attached to QTreeView
I've recently run into an issue with QTreeView/QHeaderView on Qt 4.6.2 that, although I'm loathe to blame the library, looks to me like a Qt bug. Sample code:
class TreeviewTester : public QMainWindow
TreeviewTester::TreeviewTester(QWidget parent = 0, Qt::WFlags flags = 0)
: QMainWindow(parent, flags)
QTreeView view = new QTreeView(this);
QList<QStandardItem*> items; items << new QStandardItem("Test") << new QStandardItem("Test2") << new QStandardItem("Test3"); Model = new QStandardItemModel(this); Model->appendRow(items); view->setModel(Model); view->setColumnHidden(1, true); TimerId = startTimer(0);
void timerEvent(QTimerEvent* event)
The important parts here are:
- Create a QTreeView
- Give it a model
- Make sure the model has some rows
- Hide a column on the header
- Trigger the model's layout-changed signal processing (here done with the sort call).
This program leaks memory and eventually crashes, because the QList QHeaderViewPrivate::persistentHiddenSections is being appended to every time the model throws out the about-to-change-layout signal, and never cleared. It's supposed to be emptied out in the layout-changed signal handler, but QTreeView::setModel() disconnects the signal to its header for some reason, and never passes it down as far as I can tell.
Calling QTreeView::setHeader() after QTreeView::setModel() prevents the problem, because QTreeView::setHeader() doesn't disconnect the _q_layoutChanged() signal.
Looking at the 4.7 repository, it looks like it would do the same thing.
So am I misunderstanding something about the way these views should work, or is this an actual Qt bug?
[EDIT: fixed list formatting, Volker]
Please report it as a bug in Jira. If such a simple testcase triggers the problem, then I think you're on to a real issue here.