From ceefdd6044d505a7c11b24895cafa6a293d6ceb4 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 27 Mar 2025 18:58:14 +0100 Subject: [PATCH] tst_QGraphicsLinearLayout: fix memleaks in count()/insertIrem() The tests added parent-less QGraphicsWidgets to a QGraphicsLinearLayout, which doesn't reparent them (and cannot, because it doesn't have an item as a parent itself onto which to reparent them), so the test functions leaked them. The layout items, OTOH, do get reparented, and are therefore deleted by the stack-allocated layout as its children. Since there's thus no object that could naturally be a parent for the widget items, allocate them on the stack. Since their number is variable, use QVLA, whose constructor is able to create a ... wait for it ... VLA of non-copyable-non-movable elements on the stack (we test for this, also in Qt 5, see e7c792ba7179c4c81fb2e26f66031ec81234b0d7). Amends the start of the public history. Pick-to: 6.8 6.5 5.15 Change-Id: I9b0b65efbccd1fd7e708da4d12f746d942f602fe Reviewed-by: Axel Spoerl (cherry picked from commit 374b9fd03145acac49a96abe81a253e9b482230a) Reviewed-by: Qt Cherry-pick Bot --- .../tst_qgraphicslinearlayout.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index 11931048ce5..2d9a7f0625f 100644 --- a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -12,6 +12,8 @@ #include #include +#include + class tst_QGraphicsLinearLayout : public QObject { Q_OBJECT @@ -358,8 +360,10 @@ void tst_QGraphicsLinearLayout::count() SubQGraphicsLinearLayout layout; QCOMPARE(layout.count(), 0); - for (int i = 0; i < itemCount; ++i) - layout.addItem(new QGraphicsWidget); + QVarLengthArray widgets(itemCount); + + for (auto &w : widgets) + layout.addItem(&w); QCOMPARE(layout.count(), itemCount); for (int i = 0; i < layoutCount; ++i) @@ -483,8 +487,11 @@ void tst_QGraphicsLinearLayout::insertItem() return; SubQGraphicsLinearLayout layout; - for (int i = 0; i < itemCount; ++i) - layout.addItem(new QGraphicsWidget); + + QVarLengthArray widgets(itemCount); + + for (auto &w : widgets) + layout.addItem(&w); for (int i = 0; i < layoutCount; ++i) layout.addItem(new SubQGraphicsLinearLayout);