Skip to content

Commit

Permalink
qml: Protect m_wallets in WalletQmlController with QMutex
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny9 committed Nov 27, 2024
1 parent ec3e68e commit 50c5f87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/qml/walletqmlcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand All @@ -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;
}
Expand All @@ -64,17 +68,20 @@ void WalletQmlController::unloadWallets()

void WalletQmlController::handleLoadWallet(std::unique_ptr<interfaces::Wallet> 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();
}

Expand Down
2 changes: 2 additions & 0 deletions src/qml/walletqmlcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <interfaces/node.h>
#include <interfaces/wallet.h>

#include <QMutex>
#include <QObject>
#include <QThread>
#include <memory>
Expand Down Expand Up @@ -41,6 +42,7 @@ public Q_SLOTS:
WalletQmlModel* m_selected_wallet;
QObject* m_worker;
QThread* m_worker_thread;
QMutex m_wallets_mutex;
std::vector<WalletQmlModel*> m_wallets;
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
};
Expand Down

0 comments on commit 50c5f87

Please sign in to comment.