From ecac7645cb7ea49ac7c989cabfcde460cdde5fac Mon Sep 17 00:00:00 2001 From: renbin Date: Thu, 21 Nov 2024 21:10:27 +0800 Subject: [PATCH] fix: ddialog's lambda func may cause segfault During call DDialog::exec(), the onButtonClickedClose flag is set to true by default. At this point, DDialog connect lambda function captures the **temporary variable**, and does not disconnect the signal after the exec() call ends. As a result of calling the same dialog object multiple times, other addresses may be **overwritten** by previous temporary variable writes, causing segfault (crashes). Use done() to set the return value instead of via connect lambda function. Log: Fixed a bug that may cause segfault. --- src/widgets/ddialog.cpp | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/widgets/ddialog.cpp b/src/widgets/ddialog.cpp index 815f1682f..f77fb1999 100644 --- a/src/widgets/ddialog.cpp +++ b/src/widgets/ddialog.cpp @@ -41,7 +41,7 @@ DDialogPrivate::DDialogPrivate(DDialog *qq) , iconLayout(nullptr) , contentLayout(nullptr) , buttonLayout(nullptr) - , clickedButtonIndex(0) + , clickedButtonIndex(-1) { } @@ -240,9 +240,9 @@ void DDialogPrivate::_q_onButtonClicked() if (button) { int index = buttonList.indexOf(button); q->buttonClicked(index, button->text()); + clickedButtonIndex = index; if (onButtonClickedClose) { - clickedButtonIndex = index; q->done(clickedButtonIndex); } } @@ -1109,19 +1109,7 @@ int DDialog::exec() D_D(DDialog); d->clickedButtonIndex = -1; - int clickedIndex = d->clickedButtonIndex; - - if (d->onButtonClickedClose) { - // 如果设置了WA_DeleteOnClose属性,那么在exec()中将直接delete this - // d->clickedButtonIndex中记录的数据失效,这里通过信号槽更新正确的数据 - connect(this, &DDialog::buttonClicked, this, [ &clickedIndex ] (int index, const QString &) { - clickedIndex = index; - }); - } - - int code = DAbstractDialog::exec(); - - return clickedIndex >= 0 ? clickedIndex : code; + return DAbstractDialog::exec(); } void DDialog::setCloseButtonVisible(bool closeButtonVisible) @@ -1163,10 +1151,11 @@ void DDialog::hideEvent(QHideEvent *event) void DDialog::closeEvent(QCloseEvent *event) { Q_UNUSED(event) + Q_D(DDialog); Q_EMIT aboutToClose(); - done(-1); + done(d->clickedButtonIndex); Q_EMIT visibleChanged(isVisible()); Q_EMIT closed();