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 <axel.spoerl@qt.io> (cherry picked from commit bfefcec65045ab52a705d0bf5a4eef845d63dbe8) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
4c769f92a8
commit
cf1259a40e
@ -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<bool>("deleteButton");
|
||||
@ -512,16 +487,19 @@ void tst_QButtonGroup::task209485_removeFromGroupInEventHandler()
|
||||
QFETCH(int, signalCount);
|
||||
qRegisterMetaType<QAbstractButton *>("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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user