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. QModelIndex() - parent() and SIGSEV
Forum Updated to NodeBB v4.3 + New Features

QModelIndex() - parent() and SIGSEV

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 340 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.
  • MasterQM Offline
    MasterQM Offline
    MasterQ
    wrote on last edited by
    #1

    Hi,

    I am calling the parent() method of a QModelIndex and get a segmentation fault. Yes, the index do not have a parent. I then would expect to get back the default but not a crash.

            auto backendIndex = QSqlTableModel::createIndex(meItem->backendRow(), meIndex.column());
            qDebug() << "backendindex" << toString(backendIndex);
    

    with

    QString toString(QModelIndex const &index) {
        auto row = index.row();
        auto column = index.column();
        auto ret = QString("r: %1 c: %2").arg(row).arg(column);
        try {
            auto p = index.parent(); //<== crashes here
            if (index.parent().column() != -1)
                ret += QString(" p:(%1)").arg(toString(index.parent()));
        } catch (...) {
        }
        return ret;
    }
    

    Why does parent() crashes?

    jsulmJ 1 Reply Last reply
    0
    • MasterQM Offline
      MasterQM Offline
      MasterQ
      wrote on last edited by
      #3

      Sry, I didn't think about this opportunity. Of course the trace! I assume it's Monday ;-)

      The trace is going back to QObject::parent, while the QObject is NULL. I do not have catched the case of NULL in my code.

      Thanks for pointing me to the stack trace. File closed!

      JonBJ 1 Reply Last reply
      0
      • MasterQM MasterQ

        Hi,

        I am calling the parent() method of a QModelIndex and get a segmentation fault. Yes, the index do not have a parent. I then would expect to get back the default but not a crash.

                auto backendIndex = QSqlTableModel::createIndex(meItem->backendRow(), meIndex.column());
                qDebug() << "backendindex" << toString(backendIndex);
        

        with

        QString toString(QModelIndex const &index) {
            auto row = index.row();
            auto column = index.column();
            auto ret = QString("r: %1 c: %2").arg(row).arg(column);
            try {
                auto p = index.parent(); //<== crashes here
                if (index.parent().column() != -1)
                    ret += QString(" p:(%1)").arg(toString(index.parent()));
            } catch (...) {
            }
            return ret;
        }
        

        Why does parent() crashes?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @MasterQ Please post the stack trace after the crash

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • MasterQM Offline
          MasterQM Offline
          MasterQ
          wrote on last edited by
          #3

          Sry, I didn't think about this opportunity. Of course the trace! I assume it's Monday ;-)

          The trace is going back to QObject::parent, while the QObject is NULL. I do not have catched the case of NULL in my code.

          Thanks for pointing me to the stack trace. File closed!

          JonBJ 1 Reply Last reply
          0
          • MasterQM MasterQ has marked this topic as solved on
          • MasterQM Offline
            MasterQM Offline
            MasterQ
            wrote on last edited by
            #4

            the database of the forum seems to be irritated. My last posts appear twice. first time before the post I am answering and second time after.

            1 Reply Last reply
            0
            • MasterQM MasterQ

              Sry, I didn't think about this opportunity. Of course the trace! I assume it's Monday ;-)

              The trace is going back to QObject::parent, while the QObject is NULL. I do not have catched the case of NULL in my code.

              Thanks for pointing me to the stack trace. File closed!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @MasterQ said in QModelIndex() - parent() and SIGSEV:

              The trace is going back to QObject::parent, while the QObject is NULL

              What QObject, and what NULL or nullptr? In your code neither index nor index.parent can be nullptr, they are not pointers, and nor are they QObjects. An invalid QModelIndex certianly still has valid row() & column(), not sure if it guarantees that parent() is also an invalid QModelIndex so unsure how index.parent().column() might behave.

              1 Reply Last reply
              0
              • MasterQM Offline
                MasterQM Offline
                MasterQ
                wrote on last edited by
                #6

                QModelndex::parent() internally calls parent() from its model

                    QModelIndex ProjectsBackendModel::parent(const QModelIndex &meIndex) const {
                
                        if (!meIndex.isValid())
                            return {};
                
                        auto *meItem = static_cast<ProjectsViewTreeItem *>(meIndex.internalPointer());
                
                        ProjectsViewTreeItem *parentItem = meItem ? meItem->parent() : m_root; // <= correct
                        //ProjectsViewTreeItem *parentItem = meItem->parent(); // wrong, ignoring potential nullptr of meItem
                
                        auto ret = parentItem != m_root
                                       ? createIndex(parentItem->frontendRow(), 0, parentItem)
                                       : QModelIndex{};
                        return ret;
                    }
                

                internalPointer of meIndex was nullptr , so meIndex->parent() was crashing. The type of meItem (ProjectsViewTreeItem) is inherited from QObject.

                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