From 5a559758d18627c7adb9df81ac8e0eb43eb1e7c3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 2 Apr 2025 21:00:56 +0200 Subject: [PATCH] tst_QGraphicsGridLayout: fix memleaks in columnSpacing() The populateLayout() function adds parent-less QGraphicsWidgets (RectWidgets) to the layout. If the layout had a graphics-widget parent, it would reparent them onto the parent. But it didn't, so it didn't. Therefore, the 2nd and 3rd part of the test function (as denoted by the top-level scopes in the function) leaked a lot of items. The first part of the function already used a widget, itself without parent, but added to a QGraphicsScene allocated on the stack, so was not leaking. To fix, give the layouts a graphics-widget parent. Then the layout doesn't need to be deleted anymore, either. Amends the start of the public history. Pick-to: 6.9 6.8 6.5 5.15 Change-Id: I7e63c51cf037818fb1e519dab5394ad75c74d67d Reviewed-by: Axel Spoerl --- .../qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index 186846973de..ba2ca4f8e51 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -867,6 +867,8 @@ void tst_QGraphicsGridLayout::columnSpacing() // don't include items and spacings that was previously part of the layout // (horizontal) QGraphicsGridLayout *layout = new QGraphicsGridLayout; + QGraphicsWidget parent; + parent.setLayout(layout); populateLayout(layout, 3, 1); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); @@ -879,12 +881,13 @@ void tst_QGraphicsGridLayout::columnSpacing() QCOMPARE(layout->preferredSize(), QSizeF(60, 25)); layout->removeAt(1); QCOMPARE(layout->preferredSize(), QSizeF(25, 25)); - delete layout; } { // don't include items and spacings that was previously part of the layout // (vertical) QGraphicsGridLayout *layout = new QGraphicsGridLayout; + QGraphicsWidget parent; + parent.setLayout(layout); populateLayout(layout, 2, 2); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); @@ -899,7 +902,6 @@ void tst_QGraphicsGridLayout::columnSpacing() QCOMPARE(layout->preferredSize(), QSizeF(60, 25)); layout->removeAt(1); QCOMPARE(layout->preferredSize(), QSizeF(25, 25)); - delete layout; } }