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 <axel.spoerl@qt.io>
(cherry picked from commit 374b9fd03145acac49a96abe81a253e9b482230a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-27 18:58:14 +01:00 committed by Qt Cherry-pick Bot
parent a961848c16
commit ceefdd6044

View File

@ -12,6 +12,8 @@
#include <QtWidgets/qstyle.h>
#include <QtWidgets/qproxystyle.h>
#include <QtCore/qvarlengtharray.h>
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<QGraphicsWidget, 5> 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<QGraphicsWidget> widgets(itemCount);
for (auto &w : widgets)
layout.addItem(&w);
for (int i = 0; i < layoutCount; ++i)
layout.addItem(new SubQGraphicsLinearLayout);