QModelIndex() - parent() and SIGSEV
-
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?
-
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!
-
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?
-
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!
-
M MasterQ has marked this topic as solved on
-
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!
@MasterQ said in QModelIndex() - parent() and SIGSEV:
The trace is going back to QObject::parent, while the QObject is NULL
What
QObject
, and whatNULL
ornullptr
? In your code neitherindex
norindex.parent
can benullptr
, they are not pointers, and nor are theyQObject
s. An invalid QModelIndex certianly still has validrow()
&column()
, not sure if it guarantees thatparent()
is also an invalidQModelIndex
so unsure howindex.parent().column()
might behave. -
QModelndex::parent()
internally callsparent()
from its modelQModelIndex 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
ofmeIndex
wasnullptr
, someIndex->parent()
was crashing. The type ofmeItem
(ProjectsViewTreeItem
) is inherited fromQObject
.