Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory leak in QHeaderView with hidden sections attached to QTreeView
Forum Updated to NodeBB v4.3 + New Features

Memory leak in QHeaderView with hidden sections attached to QTreeView

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jamesmpicone
    wrote on last edited by
    #1

    Hi,

    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:

    @
    #include <QStandardItemModel>
    #include <QTreeView>

    class TreeviewTester : public QMainWindow
    {
    Q_OBJECT

    public:
    TreeviewTester::TreeviewTester(QWidget parent = 0, Qt::WFlags flags = 0)
    : QMainWindow(parent, flags)
    {
    QTreeView
    view = new QTreeView(this);
    setCentralWidget(view);

    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);
    

    }

    TreeviewTester::~TreeviewTester()
    {
    killTimer(TimerId);
    }

    protected:

    void timerEvent(QTimerEvent* event)
    {
    Model->sort(0);
    }

    private:
    QStandardItemModel* Model;

    int TimerId;
    };
    @

    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]

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved