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 <Friedemann.Kleint@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2025-03-24 15:39:44 +01:00
parent 0617d177ac
commit 3651769d52

View File

@ -23,6 +23,7 @@
#include <QtCore/QDebug>
#include <QtCore/QLoggingCategory>
#include <QtCore/qscopeguard.h>
#include <private/qgraphicsscene_p.h>
#include <private/qgraphicssceneindex_p.h>
@ -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