tst_QDialogButtonBox: fix memleak in hideAndShowButton()

The old code created the top-level widget on the heap, without a
parent, so the test function leaked it.

Fix by creating on the stack instead.

This means we need to move the definition to before that of the child
button box (itself created on the stack), to avoid the double-delete
that would happen if the automatic parent would delete its QObject
child and then the compiler deletes the child again because it, too,
is an automatic variable. If, OTOH, the parent is declared before the
child, the child will have unregistered itself from its parent when it
was deleted.

Amends bbb71e7e80f292c2e69faef81b1624832981147e.

Pick-to: 6.9 6.8 6.5
Change-Id: I9489daace3bbbeecfaedcb5780a6b29a82c8d4ff
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Marc Mutz 2025-03-26 22:38:55 +01:00
parent bfefcec650
commit 8352112e58

View File

@ -389,13 +389,13 @@ void tst_QDialogButtonBox::hideAndShowButton()
if (QGuiApplication::styleHints()->tabFocusBehavior() != Qt::TabFocusAllControls)
QSKIP("Test requires Qt::TabFocusAllControls tab focus behavior");
QWidget widget;
QDialogButtonBox buttonBox;
QDialogButtonBoxPrivate *d = static_cast<QDialogButtonBoxPrivate *>(QObjectPrivate::get(&buttonBox));
auto *apply = buttonBox.addButton( "Apply", QDialogButtonBox::ApplyRole );
auto *accept = buttonBox.addButton( "Accept", QDialogButtonBox::AcceptRole );
buttonBox.addButton( "Reject", QDialogButtonBox::RejectRole );
auto *widget = new QWidget();
auto *layout = new QHBoxLayout(widget);
auto *layout = new QHBoxLayout(&widget);
layout->addWidget(&buttonBox);
// apply button shows first on macOS. accept button on all other OSes.
@ -409,8 +409,8 @@ void tst_QDialogButtonBox::hideAndShowButton()
#endif
hideButton->hide();
widget->show();
QVERIFY(QTest::qWaitForWindowExposed(widget));
widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget));
QTRY_VERIFY(buttonBox.focusWidget()); // QTBUG-114377: Without fixing, focusWidget() == nullptr
QCOMPARE(d->visibleButtons().count(), 2);
QCOMPARE(d->hiddenButtons.count(), 1);