From c1adc5c3ed1f9c6f81bb10b122aae8493f46c69f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 27 Mar 2025 21:31:17 +0100 Subject: [PATCH] tst_QGraphicsLinearLayout: fix memory leaks in removeAt()/removeItem() Like in other test functions in this class, the QGraphicsWidgets were leaked. So the solution is the same: hold them in a QVLA. Also like in other test functions, the child layouts _are_ owned by the parent layout, so were not leaked. There's a twist here, though: we're removing layout items, which may be widgets _or_ layouts. The old code deleted the removed layout item, but we can't do that anymore, since it may be one of the widgets that are owned by the QVLA, and that would cause a double-delete. If we don't delete the removed item at all anymore, though, we'd leak it when it was a layout. So hold the layouts in QVLA, too. _Then_ we don't need to delete the removed item anymore. Amends the start of the public history. Pick-to: 6.8 6.5 5.15 Change-Id: I16c61eb18f2843ceb72a432db1bd9be29e258bfd Reviewed-by: Axel Spoerl (cherry picked from commit 0665ae81fb33dc6f71f494ad6df92f85aad70ca4) Reviewed-by: Qt Cherry-pick Bot --- .../tst_qgraphicslinearlayout.cpp | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index e5d61d28be5..3a511af4598 100644 --- a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -735,10 +735,15 @@ void tst_QGraphicsLinearLayout::removeAt() return; SubQGraphicsLinearLayout layout(orientation); - for (int i = 0; i < itemCount; ++i) - layout.addItem(new QGraphicsWidget); - for (int i = 0; i < layoutCount; ++i) - layout.addItem(new SubQGraphicsLinearLayout); + + QVarLengthArray widgets(itemCount); + for (auto &w : widgets) + layout.addItem(&w); + + QVarLengthArray layouts(layoutCount); + for (auto &l : layouts) + layout.addItem(&l); + QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); QGraphicsLayoutItem *w = nullptr; @@ -750,7 +755,6 @@ void tst_QGraphicsLinearLayout::removeAt() layout.removeAt(removeItemAt); wParent = w->parentLayoutItem(); QCOMPARE(wParent, nullptr); - delete w; } QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); @@ -786,19 +790,22 @@ void tst_QGraphicsLinearLayout::removeItem() return; SubQGraphicsLinearLayout layout; - for (int i = 0; i < itemCount; ++i) - layout.addItem(new QGraphicsWidget); - for (int i = 0; i < layoutCount; ++i) - layout.addItem(new SubQGraphicsLinearLayout); + + QVarLengthArray widgets(itemCount); + for (auto &w : widgets) + layout.addItem(&w); + + QVarLengthArray layouts(layoutCount); + for (auto &l : layouts) + layout.addItem(&l); QGraphicsLayoutItem *w = nullptr; if (removeItemAt >= 0 && removeItemAt < layout.count()) w = layout.itemAt(removeItemAt); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - if (w) { + if (w) layout.removeItem(w); - delete w; - } + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate();