Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-38346

Deleting item view selection model after setModel() call is unsafe

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.6.2
    • None
    • CentOS Release 6.5 (Final) GNU/Linux Kernel 2.6.32-431.1.2.0.1.el6.i686

    Description

      Documentation recommend to delete old selection model after setModel() call:

      QItemSelectionModel *m = view->selectionModel();
      view->setModel(new_model);
      delete m;
      

      But what if old and new models are the same?

      #include <QApplication>
      
      #include <QDirModel>
      
      #include <QItemSelectionModel>
      
      #include <QTreeView>
      
      
      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
      
          QDirModel* model = new QDirModel();
      
          QTreeView* view = new QTreeView();
      
          view->setModel(model);
      
          view->show();
      
          QItemSelectionModel* old_selection_model = view->selectionModel();
      
          view->setModel(model);
      
          delete old_selection_model; // oops
      
          return app.exec();
      }
      

      Oops, we got segmentation fault: the old selection model is deleted, but the new one is not created since the old model is the same as the new model.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            docteam Qt Documentation Team
            slava_phirsov phirsov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes