From 50c5f87552b6bf9d56840b91cdf0ea58f1ec862d Mon Sep 17 00:00:00 2001 From: johnny9 <985648+johnny9@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:47:29 -0500 Subject: [PATCH] qml: Protect m_wallets in WalletQmlController with QMutex --- src/qml/walletqmlcontroller.cpp | 27 +++++++++++++++++---------- src/qml/walletqmlcontroller.h | 2 ++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/qml/walletqmlcontroller.cpp b/src/qml/walletqmlcontroller.cpp index 7502c189db..25941cb3e2 100644 --- a/src/qml/walletqmlcontroller.cpp +++ b/src/qml/walletqmlcontroller.cpp @@ -41,8 +41,11 @@ void WalletQmlController::setSelectedWallet(QString path) if (wallet.has_value()) { auto wallet_model = new WalletQmlModel(std::move(wallet.value())); wallet_model->moveToThread(this->thread()); - m_selected_wallet = wallet_model; - m_wallets.push_back(m_selected_wallet); + { + QMutexLocker locker(&m_wallets_mutex); + m_selected_wallet = wallet_model; + m_wallets.push_back(m_selected_wallet); + } Q_EMIT selectedWalletChanged(); } }); @@ -56,6 +59,7 @@ WalletQmlModel* WalletQmlController::selectedWallet() const void WalletQmlController::unloadWallets() { m_handler_load_wallet->disconnect(); + QMutexLocker locker(&m_wallets_mutex); for (WalletQmlModel* wallet : m_wallets) { delete wallet; } @@ -64,17 +68,20 @@ void WalletQmlController::unloadWallets() void WalletQmlController::handleLoadWallet(std::unique_ptr wallet) { - if (!m_wallets.empty()) { - QString name = QString::fromStdString(wallet->getWalletName()); - for (WalletQmlModel* wallet_model : m_wallets) { - if (wallet_model->name() == name) { - return; + { + QMutexLocker locker(&m_wallets_mutex); + if (!m_wallets.empty()) { + QString name = QString::fromStdString(wallet->getWalletName()); + for (WalletQmlModel* wallet_model : m_wallets) { + if (wallet_model->name() == name) { + return; + } } } - } - m_selected_wallet = new WalletQmlModel(std::move(wallet)); - m_wallets.push_back(m_selected_wallet); + m_selected_wallet = new WalletQmlModel(std::move(wallet)); + m_wallets.push_back(m_selected_wallet); + } Q_EMIT selectedWalletChanged(); } diff --git a/src/qml/walletqmlcontroller.h b/src/qml/walletqmlcontroller.h index fb15debd07..bd2e16d531 100644 --- a/src/qml/walletqmlcontroller.h +++ b/src/qml/walletqmlcontroller.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -41,6 +42,7 @@ public Q_SLOTS: WalletQmlModel* m_selected_wallet; QObject* m_worker; QThread* m_worker_thread; + QMutex m_wallets_mutex; std::vector m_wallets; std::unique_ptr m_handler_load_wallet; };