From 45387aa6631171276e708dc2003333296dcb273a Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 21 Dec 2023 00:24:16 -0800 Subject: [PATCH] Qt: Fix Action leak --- src/platform/qt/ActionMapper.cpp | 11 +++++++++-- src/platform/qt/ConfigController.cpp | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/platform/qt/ActionMapper.cpp b/src/platform/qt/ActionMapper.cpp index cb96ab6dc..890ea8893 100644 --- a/src/platform/qt/ActionMapper.cpp +++ b/src/platform/qt/ActionMapper.cpp @@ -93,7 +93,13 @@ void ActionMapper::rebuildMenu(const QString& menu, QMenu* qmenu, QWidget* conte qaction->setMenuRole(QAction::QuitRole); break; } - QObject::connect(qaction, &QAction::triggered, [qaction, action](bool enabled) { + + std::weak_ptr weakAction(action); + QObject::connect(qaction, &QAction::triggered, [qaction, weakAction](bool enabled) { + if (weakAction.expired()) { + return; + } + std::shared_ptr action(weakAction.lock()); if (qaction->isCheckable()) { action->trigger(enabled); } else { @@ -101,7 +107,8 @@ void ActionMapper::rebuildMenu(const QString& menu, QMenu* qmenu, QWidget* conte } }); QObject::connect(action.get(), &Action::enabled, qaction, &QAction::setEnabled); - QObject::connect(action.get(), &Action::activated, [qaction, action](bool active) { + QObject::connect(action.get(), &Action::activated, [qaction, weakAction](bool active) { + std::shared_ptr action(weakAction.lock()); if (qaction->isCheckable()) { qaction->setChecked(active); } else if (active) { diff --git a/src/platform/qt/ConfigController.cpp b/src/platform/qt/ConfigController.cpp index 7a26ea844..8922830fc 100644 --- a/src/platform/qt/ConfigController.cpp +++ b/src/platform/qt/ConfigController.cpp @@ -49,7 +49,12 @@ std::shared_ptr ConfigOption::addValue(const QString& text, const QVaria action = std::make_shared(function, name, text, this); } action->setExclusive(); - QObject::connect(action.get(), &QObject::destroyed, this, [this, action, value]() { + std::weak_ptr weakAction(action); + QObject::connect(action.get(), &QObject::destroyed, this, [this, weakAction, value]() { + if (weakAction.expired()) { + return; + } + std::shared_ptr action(weakAction.lock()); m_actions.removeAll(std::make_pair(action, value)); }); m_actions.append(std::make_pair(action, value)); @@ -71,7 +76,12 @@ std::shared_ptr ConfigOption::addBoolean(const QString& text, ActionMapp action = std::make_shared(function, m_name, text, this); } - QObject::connect(action.get(), &QObject::destroyed, this, [this, action]() { + std::weak_ptr weakAction(action); + QObject::connect(action.get(), &QObject::destroyed, this, [this, weakAction]() { + if (weakAction.expired()) { + return; + } + std::shared_ptr action(weakAction.lock()); m_actions.removeAll(std::make_pair(action, QVariant(1))); }); m_actions.append(std::make_pair(action, QVariant(1)));