From cf1259a40e6b738a12bb5c5411aaa069cc02ad9f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 26 Mar 2025 22:06:30 +0100 Subject: [PATCH] tst_QButtonGroup: fix memleak in task209485_removeFromGroupInEventHandler() A QButtonGroup does not own the buttons it contains, so this test function leaked the button, despite a thing called "ButtonDeleter". That thing doesn't _actually_ delete the button, it merely removes it from the group. To prevent the code from instilling same confusion in the next reader as it did in this one, inline the whole thing and condense it down to a single connect() statement, using a lambda. Sometimes, another level of indirection (or two) does _not_ solve problems... Amends the start of the public history. Pick-to: 6.8 6.5 5.15 Change-Id: I0002f0dd86d505d1aa089d696776051fddbf4c1e Reviewed-by: Axel Spoerl (cherry picked from commit bfefcec65045ab52a705d0bf5a4eef845d63dbe8) Reviewed-by: Qt Cherry-pick Bot --- .../widgets/qbuttongroup/tst_qbuttongroup.cpp | 36 ++++--------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp b/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp index 46eb4416bf5..172e9fe7ac1 100644 --- a/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp +++ b/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp @@ -473,31 +473,6 @@ void tst_QButtonGroup::checkedButton() QCOMPARE(buttons.checkedButton(), &pb2); } -class task209485_ButtonDeleter : public QObject -{ - Q_OBJECT - -public: - task209485_ButtonDeleter(QButtonGroup *group, bool deleteButton) - : group(group) - , deleteButton(deleteButton) - { - connect(group, &QButtonGroup::buttonClicked, - this, &task209485_ButtonDeleter::buttonClicked); - } - -private slots: - void buttonClicked() - { - if (deleteButton) - group->removeButton(group->buttons().first()); - } - -private: - QButtonGroup *group; - bool deleteButton; -}; - void tst_QButtonGroup::task209485_removeFromGroupInEventHandler_data() { QTest::addColumn("deleteButton"); @@ -512,16 +487,19 @@ void tst_QButtonGroup::task209485_removeFromGroupInEventHandler() QFETCH(int, signalCount); qRegisterMetaType("QAbstractButton *"); - TestPushButton *button = new TestPushButton; + TestPushButton button; QButtonGroup group; - group.addButton(button); + group.addButton(&button); - task209485_ButtonDeleter buttonDeleter(&group, deleteButton); + if (deleteButton) { + QObject::connect(&group, &QButtonGroup::buttonClicked, + &button, [&] { group.removeButton(&button); }); + } QSignalSpy spy1(&group, SIGNAL(buttonClicked(QAbstractButton*))); // NOTE: Reintroducing the bug of this task will cause the following line to crash: - QTest::mouseClick(button, Qt::LeftButton); + QTest::mouseClick(&button, Qt::LeftButton); QCOMPARE(spy1.size(), signalCount); }