Qt: Fix Action leak

This commit is contained in:
Vicki Pfau 2023-12-21 00:24:16 -08:00
parent ffacbcfeea
commit 45387aa663
2 changed files with 21 additions and 4 deletions

View File

@ -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<Action> weakAction(action);
QObject::connect(qaction, &QAction::triggered, [qaction, weakAction](bool enabled) {
if (weakAction.expired()) {
return;
}
std::shared_ptr<Action> 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> action(weakAction.lock());
if (qaction->isCheckable()) {
qaction->setChecked(active);
} else if (active) {

View File

@ -49,7 +49,12 @@ std::shared_ptr<Action> ConfigOption::addValue(const QString& text, const QVaria
action = std::make_shared<Action>(function, name, text, this);
}
action->setExclusive();
QObject::connect(action.get(), &QObject::destroyed, this, [this, action, value]() {
std::weak_ptr<Action> weakAction(action);
QObject::connect(action.get(), &QObject::destroyed, this, [this, weakAction, value]() {
if (weakAction.expired()) {
return;
}
std::shared_ptr<Action> 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<Action> ConfigOption::addBoolean(const QString& text, ActionMapp
action = std::make_shared<Action>(function, m_name, text, this);
}
QObject::connect(action.get(), &QObject::destroyed, this, [this, action]() {
std::weak_ptr<Action> weakAction(action);
QObject::connect(action.get(), &QObject::destroyed, this, [this, weakAction]() {
if (weakAction.expired()) {
return;
}
std::shared_ptr<Action> action(weakAction.lock());
m_actions.removeAll(std::make_pair(action, QVariant(1)));
});
m_actions.append(std::make_pair(action, QVariant(1)));