From 3651769d52108b01b142a3bcaae530e949eafa68 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Mar 2025 15:39:44 +0100 Subject: [PATCH] tst_QGraphicsScene: fix memleaks in selectionChanged()/removeItem() A QGraphicsScene owns the items added to it, but if we later removeItem() one again, then we're also responsible for freeing the item. The test functions didn't do that, so we leaked the item. In order to not change the rest of the functions by deleting the item earlier than it had been before, install a qScopeGuard() to delete it, right after we removed it from the scene again. Amends the start of the public history. Pick-to: 6.9 6.8 6.5 5.15 Change-Id: I8ab50626a0b5d52825bb329233ab8efe6c1291dc Reviewed-by: Friedemann Kleint Reviewed-by: Volker Hilsheimer --- .../widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp index 0f5882bf6a5..3e8b3401370 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -986,6 +987,7 @@ void tst_QGraphicsScene::selectionChanged() QCOMPARE(spy.size(), 3); // the grandchild was added, so the selection changed once scene.removeItem(parentItem); + const auto reaper = qScopeGuard([=] { delete parentItem; }); QCOMPARE(spy.size(), 4); // the grandchild was removed, so the selection changed rect->setSelected(true); @@ -1279,6 +1281,7 @@ void tst_QGraphicsScene::removeItem() item2->setSelected(true); QVERIFY(scene.selectedItems().contains(item2)); scene.removeItem(item2); + const auto reaper = qScopeGuard([=] { delete item2; }); QVERIFY(scene.selectedItems().isEmpty()); // Check that we are in a state that can receive paint events