QToolButton: Don't crash if deleted while in event handler
If QToolButton::mouseReleaseEvent triggers its deletion then it will crash when dereferencing its d pointer. qabstractbutton.cpp already uses QPointer guards in many places but qtoolbutton.cpp was missing this one. While deleteLater() is still our recommendation, we shouldn't crash. It's not always obvious what led to the button's destruction, as the chain of indirections can be long. Change-Id: I4a33447fa4e90953370277eb57a161398ded9a9c Pick-to: 6.7 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>bb10
parent
d4f2a5aa40
commit
97d227acc7
|
|
@ -584,8 +584,10 @@ void QToolButton::mousePressEvent(QMouseEvent *e)
|
|||
void QToolButton::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
Q_D(QToolButton);
|
||||
QPointer<QAbstractButton> guard(this);
|
||||
QAbstractButton::mouseReleaseEvent(e);
|
||||
d->buttonPressed = QToolButtonPrivate::NoButtonPressed;
|
||||
if (guard)
|
||||
d->buttonPressed = QToolButtonPrivate::NoButtonPressed;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ private slots:
|
|||
void qtbug_26956_popupTimerDone();
|
||||
void qtbug_34759_sizeHintResetWhenSettingMenu();
|
||||
void defaultActionSynced();
|
||||
void deleteInHandler();
|
||||
|
||||
protected slots:
|
||||
void sendMouseClick();
|
||||
|
|
@ -316,5 +317,22 @@ void tst_QToolButton::defaultActionSynced()
|
|||
QCOMPARE(bSpy.size(), ++bToggledCount);
|
||||
}
|
||||
|
||||
void tst_QToolButton::deleteInHandler()
|
||||
{
|
||||
// Tests that if something deletes the button
|
||||
// while its event handler is still on the callstack, we don't crash
|
||||
|
||||
QPointer<QToolButton> tb = new QToolButton();
|
||||
tb->show();
|
||||
QVERIFY(QTest::qWaitForWindowActive(tb));
|
||||
|
||||
connect(tb, &QToolButton::clicked, this, [tb] {
|
||||
delete tb;
|
||||
});
|
||||
|
||||
QTest::mouseClick(tb, Qt::LeftButton);
|
||||
QVERIFY(!tb);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QToolButton)
|
||||
#include "tst_qtoolbutton.moc"
|
||||
|
|
|
|||
Loading…
Reference in New Issue