tst_QAbstractItemView: fix memleaks in testDelegateDestroyEditorChild()

After removing the QTreeWidgetItems from the QTreeWidget, they have no
parent anymore, and so no-one deletes them and they're leaked.

Add a vector<unique_ptr> to hold such items until the end of the
function. This is the minimally-invasive fix. If we were to delete the
items right after removeChild(), we might be changing the test, and if
we deleted them only at the end of the function, we'd still be leaking
them on a failed QCOMPARE().

Amends 1d799e91082092821a04885bd9d069febefc37da.

This doesn't completely fix tst_QAbstractItemView; it still suffers
from the QScroller leak, QTBUG-135055.

Pick-to: 6.8 6.5
Task-number: QTBUG-135055
Change-Id: I2d3c661d5331ae33bb945f850fccdaa934f7a2dd
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 9ea9818eeb29e79e7f12505a21669902d443dc1f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-26 09:57:06 +01:00 committed by Qt Cherry-pick Bot
parent 2ec89d4908
commit 2c9baee470

View File

@ -32,6 +32,9 @@
#include <private/qabstractitemview_p.h> #include <private/qabstractitemview_p.h>
#include <QtWidgets/private/qapplication_p.h> #include <QtWidgets/private/qapplication_p.h>
#include <vector>
#include <memory>
Q_DECLARE_METATYPE(Qt::ItemFlags); Q_DECLARE_METATYPE(Qt::ItemFlags);
using namespace QTestPrivate; using namespace QTestPrivate;
@ -1629,6 +1632,7 @@ void tst_QAbstractItemView::testDelegateDestroyEditor()
void tst_QAbstractItemView::testDelegateDestroyEditorChild() void tst_QAbstractItemView::testDelegateDestroyEditorChild()
{ {
std::vector<std::unique_ptr<QTreeWidgetItem>> reaper;
QTreeWidget tree; QTreeWidget tree;
MyAbstractItemDelegate delegate; MyAbstractItemDelegate delegate;
tree.setItemDelegate(&delegate); tree.setItemDelegate(&delegate);
@ -1644,8 +1648,10 @@ void tst_QAbstractItemView::testDelegateDestroyEditorChild()
tree.openPersistentEditor(levelTwo2); tree.openPersistentEditor(levelTwo2);
QCOMPARE(delegate.virtualCtorCallCount, 4); QCOMPARE(delegate.virtualCtorCallCount, 4);
levelOne1->removeChild(levelTwo1); levelOne1->removeChild(levelTwo1);
reaper.emplace_back(levelTwo1);
QCOMPARE(delegate.virtualDtorCallCount, 1); QCOMPARE(delegate.virtualDtorCallCount, 1);
topLevel->removeChild(levelOne2); topLevel->removeChild(levelOne2);
reaper.emplace_back(levelOne2);
QCOMPARE(delegate.virtualDtorCallCount, 3); QCOMPARE(delegate.virtualDtorCallCount, 3);
} }