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 <axel.spoerl@qt.io>
(cherry picked from commit 0665ae81fb33dc6f71f494ad6df92f85aad70ca4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-27 21:31:17 +01:00 committed by Qt Cherry-pick Bot
parent 97bacdaab3
commit c1adc5c3ed

View File

@ -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<QGraphicsWidget> widgets(itemCount);
for (auto &w : widgets)
layout.addItem(&w);
QVarLengthArray<SubQGraphicsLinearLayout> 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<QGraphicsWidget> widgets(itemCount);
for (auto &w : widgets)
layout.addItem(&w);
QVarLengthArray<SubQGraphicsLinearLayout> 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();