From a961848c164e1cd14f38731c20aefcfe64dbfa1c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 27 Mar 2025 18:45:00 +0100 Subject: [PATCH] tst_QGraphicsLinearLayout: fix memleaks in itemAt() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old code used a data-driven test to check that itemAt(i), 0 ≤ i ≤ 2, was returning non-nullptr. It re-created the widgets and the layout for each data tag anew, leaking the widgets by the way. Using data-driven test machinery here is overkill. Remove it and create the widgets in a C array on the stack, so they're not leaked, and iterate over them using range-for. Also check not just for nullptr, but also that the pointer matches the widgets we put into layout, at the position we put them in. Amends the start of the public history. Pick-to: 6.8 6.5 5.15 Change-Id: Ib808aa6756c6550a61a4768edfc5021f2f6ae3b3 Reviewed-by: Axel Spoerl (cherry picked from commit 159029b58753ad8a76eef9ea105275001a430ef2) Reviewed-by: Qt Cherry-pick Bot --- .../tst_qgraphicslinearlayout.cpp | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index c11a3bc53dc..11931048ce5 100644 --- a/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -34,7 +34,6 @@ private slots: void insertStretch(); void invalidate_data(); void invalidate(); - void itemAt_data(); void itemAt(); void itemAt_visualOrder(); void orientation_data(); @@ -633,24 +632,19 @@ void tst_QGraphicsLinearLayout::invalidate() delete widget; } -void tst_QGraphicsLinearLayout::itemAt_data() -{ - QTest::addColumn("index"); - QTest::newRow("0") << 0; - QTest::newRow("1") << 1; - QTest::newRow("2") << 2; -} - // QGraphicsLayoutItem* itemAt(int index) const public void tst_QGraphicsLinearLayout::itemAt() { // see also the insertItem() etc tests - QFETCH(int, index); SubQGraphicsLinearLayout layout; - for (int i = 0; i < 3; ++i) - layout.addItem(new QGraphicsWidget); - QVERIFY(layout.itemAt(index) != 0); + QGraphicsWidget widgets[3]; + for (auto &w : widgets) + layout.addItem(&w); + + int i = 0; + for (const auto &w : widgets) + QCOMPARE(layout.itemAt(i++), &w); } void tst_QGraphicsLinearLayout::itemAt_visualOrder()