From 9fb734607e9e6dc0103b554ae3d81caeac001787 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Sat, 3 Jul 2021 03:29:13 +0200 Subject: [PATCH] WindowManager: move Wallet cleanup to different thread --- src/MainWindow.cpp | 13 +++++------ src/MainWindow.h | 2 ++ src/WindowManager.cpp | 6 ++++++ src/WindowManager.h | 2 ++ src/appcontext.cpp | 32 ++++++++++++++++------------ src/appcontext.h | 4 +++- src/dialog/AccountSwitcherDialog.cpp | 2 +- src/dialog/TxInfoDialog.cpp | 2 +- 8 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index dadde6e..0e7cc22 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -368,8 +368,8 @@ void MainWindow::initWalletContext() { connect(m_ctx->nodes, &Nodes::WSNodeExhausted, this, &MainWindow::showWSNodeExhaustedMessage); // Wallet - connect(m_ctx->wallet.get(), &Wallet::connectionStatusChanged, this, &MainWindow::onConnectionStatusChanged); - connect(m_ctx->wallet.get(), &Wallet::currentSubaddressAccountChanged, this, &MainWindow::updateTitle); + connect(m_ctx->wallet, &Wallet::connectionStatusChanged, this, &MainWindow::onConnectionStatusChanged); + connect(m_ctx->wallet, &Wallet::currentSubaddressAccountChanged, this, &MainWindow::updateTitle); } void MainWindow::menuToggleTabVisible(const QString &key){ @@ -714,7 +714,7 @@ void MainWindow::showConnectionStatusDialog() { } void MainWindow::showPasswordDialog() { - PasswordChangeDialog dialog{this, m_ctx->wallet.get()}; + PasswordChangeDialog dialog{this, m_ctx->wallet}; dialog.exec(); this->updatePasswordIcon(); } @@ -790,12 +790,12 @@ void MainWindow::menuSettingsClicked() { } void MainWindow::menuSignVerifyClicked() { - SignVerifyDialog dialog{m_ctx->wallet.get(), this}; + SignVerifyDialog dialog{m_ctx->wallet, this}; dialog.exec(); } void MainWindow::menuVerifyTxProof() { - VerifyProofDialog dialog{m_ctx->wallet.get(), this}; + VerifyProofDialog dialog{m_ctx->wallet, this}; dialog.exec(); } @@ -845,6 +845,7 @@ void MainWindow::closeEvent(QCloseEvent *event) { m_txTimer.stop(); this->saveGeo(); + m_ctx->stopTimers(); m_windowManager->closeWindow(this); } @@ -1175,7 +1176,7 @@ void MainWindow::rescanSpent() { } void MainWindow::showBalanceDialog() { - BalanceDialog dialog{this, m_ctx->wallet.get()}; + BalanceDialog dialog{this, m_ctx->wallet}; dialog.exec(); } diff --git a/src/MainWindow.h b/src/MainWindow.h index 3d8557d..212b62d 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -179,6 +179,8 @@ private slots: void onSetStatusText(const QString &text); private: + friend WindowManager; + void initStatusBar(); void initWidgets(); void initMenu(); diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index 52177bc..21fee86 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -17,6 +17,7 @@ WindowManager::WindowManager() { m_walletManager = WalletManager::instance(); m_splashDialog = new SplashDialog; + m_cleanupThread = new QThread(); connect(m_walletManager, &WalletManager::walletOpened, this, &WindowManager::onWalletOpened); connect(m_walletManager, &WalletManager::walletCreated, this, &WindowManager::onWalletCreated); @@ -68,6 +69,11 @@ void WindowManager::close() { void WindowManager::closeWindow(MainWindow *window) { m_windows.removeOne(window); + + // Move Wallet to a different thread for cleanup so it doesn't block GUI thread + window->m_ctx->wallet->moveToThread(m_cleanupThread); + m_cleanupThread->start(); + window->m_ctx->wallet->deleteLater(); } void WindowManager::restartApplication(const QString &binaryFilename) { diff --git a/src/WindowManager.h b/src/WindowManager.h index 2a87469..c82c81b 100644 --- a/src/WindowManager.h +++ b/src/WindowManager.h @@ -77,6 +77,8 @@ private: bool m_openWalletTriedOnce = false; bool m_openingWallet = false; bool m_initialNetworkConfigured = false; + + QThread *m_cleanupThread; }; diff --git a/src/appcontext.cpp b/src/appcontext.cpp index df66300..a7c1dec 100644 --- a/src/appcontext.cpp +++ b/src/appcontext.cpp @@ -27,22 +27,22 @@ AppContext::AppContext(Wallet *wallet) , networkType(constants::networkType) , m_rpc(new DaemonRpc{this, getNetworkTor(), ""}) { - connect(this->wallet.get(), &Wallet::moneySpent, this, &AppContext::onMoneySpent); - connect(this->wallet.get(), &Wallet::moneyReceived, this, &AppContext::onMoneyReceived); - connect(this->wallet.get(), &Wallet::unconfirmedMoneyReceived, this, &AppContext::onUnconfirmedMoneyReceived); - connect(this->wallet.get(), &Wallet::newBlock, this, &AppContext::onWalletNewBlock); - connect(this->wallet.get(), &Wallet::updated, this, &AppContext::onWalletUpdate); - connect(this->wallet.get(), &Wallet::refreshed, this, &AppContext::onWalletRefreshed); - connect(this->wallet.get(), &Wallet::transactionCommitted, this, &AppContext::onTransactionCommitted); - connect(this->wallet.get(), &Wallet::heightRefreshed, this, &AppContext::onHeightRefreshed); - connect(this->wallet.get(), &Wallet::transactionCreated, this, &AppContext::onTransactionCreated); - connect(this->wallet.get(), &Wallet::deviceError, this, &AppContext::onDeviceError); - connect(this->wallet.get(), &Wallet::deviceButtonRequest, this, &AppContext::onDeviceButtonRequest); - connect(this->wallet.get(), &Wallet::deviceButtonPressed, this, &AppContext::onDeviceButtonPressed); - connect(this->wallet.get(), &Wallet::connectionStatusChanged, [this]{ + connect(this->wallet, &Wallet::moneySpent, this, &AppContext::onMoneySpent); + connect(this->wallet, &Wallet::moneyReceived, this, &AppContext::onMoneyReceived); + connect(this->wallet, &Wallet::unconfirmedMoneyReceived, this, &AppContext::onUnconfirmedMoneyReceived); + connect(this->wallet, &Wallet::newBlock, this, &AppContext::onWalletNewBlock); + connect(this->wallet, &Wallet::updated, this, &AppContext::onWalletUpdate); + connect(this->wallet, &Wallet::refreshed, this, &AppContext::onWalletRefreshed); + connect(this->wallet, &Wallet::transactionCommitted, this, &AppContext::onTransactionCommitted); + connect(this->wallet, &Wallet::heightRefreshed, this, &AppContext::onHeightRefreshed); + connect(this->wallet, &Wallet::transactionCreated, this, &AppContext::onTransactionCreated); + connect(this->wallet, &Wallet::deviceError, this, &AppContext::onDeviceError); + connect(this->wallet, &Wallet::deviceButtonRequest, this, &AppContext::onDeviceButtonRequest); + connect(this->wallet, &Wallet::deviceButtonPressed, this, &AppContext::onDeviceButtonPressed); + connect(this->wallet, &Wallet::connectionStatusChanged, [this]{ this->nodes->autoConnect(); }); - connect(this->wallet.get(), &Wallet::currentSubaddressAccountChanged, [this]{ + connect(this->wallet, &Wallet::currentSubaddressAccountChanged, [this]{ this->updateBalance(); }); @@ -273,6 +273,10 @@ void AppContext::onOpenAliasResolve(const QString &openAlias) { emit openAliasResolveError(msg); } +void AppContext::stopTimers() { + m_storeTimer.stop(); +} + // ########################################## LIBWALLET QT SIGNALS #################################################### void AppContext::onMoneySpent(const QString &txId, quint64 amount) { diff --git a/src/appcontext.h b/src/appcontext.h index 093c01c..39f9f6f 100644 --- a/src/appcontext.h +++ b/src/appcontext.h @@ -25,7 +25,7 @@ Q_OBJECT public: explicit AppContext(Wallet *wallet); - QScopedPointer wallet; + Wallet *wallet; Nodes *nodes; bool donationSending = false; @@ -46,6 +46,8 @@ public: void storeWallet(); + void stopTimers(); + public slots: void onCreateTransaction(const QString &address, quint64 amount, const QString &description, bool all); void onCreateTransactionMultiDest(const QVector &addresses, const QVector &amounts, const QString &description); diff --git a/src/dialog/AccountSwitcherDialog.cpp b/src/dialog/AccountSwitcherDialog.cpp index 5d28f49..ee1e10e 100644 --- a/src/dialog/AccountSwitcherDialog.cpp +++ b/src/dialog/AccountSwitcherDialog.cpp @@ -45,7 +45,7 @@ AccountSwitcherDialog::AccountSwitcherDialog(QSharedPointer ctx, QWi m_ctx->wallet->subaddressAccount()->refresh(); }); - connect(m_ctx->wallet.get(), &Wallet::currentSubaddressAccountChanged, this, &AccountSwitcherDialog::updateSelection); + connect(m_ctx->wallet, &Wallet::currentSubaddressAccountChanged, this, &AccountSwitcherDialog::updateSelection); connect(m_ctx->wallet->subaddressAccount(), &SubaddressAccount::refreshFinished, this, &AccountSwitcherDialog::updateSelection); this->updateSelection(); diff --git a/src/dialog/TxInfoDialog.cpp b/src/dialog/TxInfoDialog.cpp index 80ba5af..f1df988 100644 --- a/src/dialog/TxInfoDialog.cpp +++ b/src/dialog/TxInfoDialog.cpp @@ -30,7 +30,7 @@ TxInfoDialog::TxInfoDialog(QSharedPointer ctx, TransactionInfo *txIn connect(ui->btn_CopyTxKey, &QPushButton::pressed, this, &TxInfoDialog::copyTxKey); connect(ui->btn_createTxProof, &QPushButton::pressed, this, &TxInfoDialog::createTxProof); - connect(m_ctx->wallet.get(), &Wallet::newBlock, this, &TxInfoDialog::updateData); + connect(m_ctx->wallet, &Wallet::newBlock, this, &TxInfoDialog::updateData); this->setData(txInfo);