tst_QTreeView: fix memleak in fetchUntilScreenFull()

The TreeModel class was lacking the destructor, so the m_root TreeItem
was never deleted (it was held in a non-owning pointer and had no
parent).

Instead of adding ~TreeModel(), port to holding m_root in unique_ptr
instead. This creates a bit more churn, but communicates intent
better, and makes sure the item is never without an owner.

Amends e74af68654c0eb127277c73e20bda409b83d157b.

As a drive-by, fix the indent of the else clause of a ternary.

Pick-to: 6.8 6.5 5.15
Change-Id: I2f5c8852b8bee89c63933c118a4599d6a17de8e2
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 7cf8b2d0303ea81a8e22fcaee7db7bb7303d0376)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-26 16:08:54 +01:00 committed by Qt Cherry-pick Bot
parent 81451de0e6
commit e215b930e2

View File

@ -5091,7 +5091,7 @@ void tst_QTreeView::fetchUntilScreenFull()
rootData.append(rootData1);
rootData.append(rootData2);
m_root = new TreeItem(rootData, nullptr);
m_root = std::make_unique<TreeItem>(rootData, nullptr);
QVariant childData1("Col 1");
QVariant childData2("Col 2");
@ -5099,7 +5099,7 @@ void tst_QTreeView::fetchUntilScreenFull()
childData.append(childData1);
childData.append(childData2);
TreeItem* item_1 = new TreeItem(childData, m_root);
TreeItem* item_1 = new TreeItem(childData, m_root.get());
m_root->children.append(item_1);
TreeItem* item_2 = new TreeItem(childData, item_1);
@ -5113,7 +5113,7 @@ void tst_QTreeView::fetchUntilScreenFull()
return QModelIndex();
TreeItem* parentItem =
parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_root;
parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_root.get();
TreeItem* childItem = parentItem->children.at(row);
return createIndex(row, column, childItem);
}
@ -5124,7 +5124,7 @@ void tst_QTreeView::fetchUntilScreenFull()
return 0;
TreeItem* parentItem = parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer())
: m_root;
: m_root.get();
return parentItem->children.size();
}
@ -5137,7 +5137,7 @@ void tst_QTreeView::fetchUntilScreenFull()
TreeItem* parentItem =
static_cast<TreeItem*>(childIndex.internalPointer())->parent;
return parentItem == m_root ? QModelIndex()
return parentItem == m_root.get() ? QModelIndex()
: createIndex(parentItem->rowInParent(), 0, parentItem);
}
@ -5201,7 +5201,7 @@ void tst_QTreeView::fetchUntilScreenFull()
QVector<TreeItem*> children;
TreeItem* parent = nullptr;
};
TreeItem* m_root;
std::unique_ptr<TreeItem> m_root;
};
if (QGuiApplication::platformName().startsWith(QLatin1String("eglfs"), Qt::CaseInsensitive))