diff --git a/src/libwalletqt/WalletManager.cpp b/src/libwalletqt/WalletManager.cpp index 37f04f71..0ff8d74d 100644 --- a/src/libwalletqt/WalletManager.cpp +++ b/src/libwalletqt/WalletManager.cpp @@ -7,10 +7,11 @@ #include #include #include +#include +#include WalletManager * WalletManager::m_instance = nullptr; - WalletManager *WalletManager::instance() { if (!m_instance) { @@ -23,27 +24,37 @@ WalletManager *WalletManager::instance() Wallet *WalletManager::createWallet(const QString &path, const QString &password, const QString &language, bool testnet) { + QMutexLocker locker(&m_mutex); + if (m_currentWallet) { + qDebug() << "Closing open m_currentWallet" << m_currentWallet; + delete m_currentWallet; + } Monero::Wallet * w = m_pimpl->createWallet(path.toStdString(), password.toStdString(), language.toStdString(), testnet); - Wallet * wallet = new Wallet(w); - return wallet; + m_currentWallet = new Wallet(w); + return m_currentWallet; } Wallet *WalletManager::openWallet(const QString &path, const QString &password, bool testnet) { + QMutexLocker locker(&m_mutex); + if (m_currentWallet) { + qDebug() << "Closing open m_currentWallet" << m_currentWallet; + delete m_currentWallet; + } qDebug("%s: opening wallet at %s, testnet = %d ", __PRETTY_FUNCTION__, qPrintable(path), testnet); Monero::Wallet * w = m_pimpl->openWallet(path.toStdString(), password.toStdString(), testnet); qDebug("%s: opened wallet: %s, status: %d", __PRETTY_FUNCTION__, w->address().c_str(), w->status()); - Wallet * wallet = new Wallet(w); + m_currentWallet = new Wallet(w); // move wallet to the GUI thread. Otherwise it wont be emitting signals - if (wallet->thread() != qApp->thread()) { - wallet->moveToThread(qApp->thread()); + if (m_currentWallet->thread() != qApp->thread()) { + m_currentWallet->moveToThread(qApp->thread()); } - return wallet; + return m_currentWallet; } void WalletManager::openWalletAsync(const QString &path, const QString &password, bool testnet) @@ -63,23 +74,34 @@ void WalletManager::openWalletAsync(const QString &path, const QString &password Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, bool testnet, quint64 restoreHeight) { + QMutexLocker locker(&m_mutex); + if (m_currentWallet) { + qDebug() << "Closing open m_currentWallet" << m_currentWallet; + delete m_currentWallet; + } Monero::Wallet * w = m_pimpl->recoveryWallet(path.toStdString(), memo.toStdString(), testnet, restoreHeight); - Wallet * wallet = new Wallet(w); - return wallet; + m_currentWallet = new Wallet(w); + return m_currentWallet; } -QString WalletManager::closeWallet(Wallet *wallet) +QString WalletManager::closeWallet() { - QString result = wallet->address(); - delete wallet; + QMutexLocker locker(&m_mutex); + QString result; + if (m_currentWallet) { + result = m_currentWallet->address(); + delete m_currentWallet; + } else { + qCritical() << "Trying to close non existing wallet " << m_currentWallet; + result = "0"; + } return result; } -void WalletManager::closeWalletAsync(Wallet *wallet) +void WalletManager::closeWalletAsync() { - QFuture future = QtConcurrent::run(this, &WalletManager::closeWallet, - wallet); + QFuture future = QtConcurrent::run(this, &WalletManager::closeWallet); QFutureWatcher * watcher = new QFutureWatcher(); watcher->setFuture(future); diff --git a/src/libwalletqt/WalletManager.h b/src/libwalletqt/WalletManager.h index 7dcff8fa..88425f6f 100644 --- a/src/libwalletqt/WalletManager.h +++ b/src/libwalletqt/WalletManager.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include class Wallet; namespace Monero { @@ -52,17 +54,15 @@ public: bool testnet = false, quint64 restoreHeight = 0); /*! - * \brief closeWallet - closes wallet and frees memory - * \param wallet + * \brief closeWallet - closes current open wallet and frees memory * \return wallet address */ - Q_INVOKABLE QString closeWallet(Wallet * wallet); + Q_INVOKABLE QString closeWallet(); /*! * \brief closeWalletAsync - asynchronous version of "closeWallet" - * \param wallet - wallet pointer; */ - Q_INVOKABLE void closeWalletAsync(Wallet * wallet); + Q_INVOKABLE void closeWalletAsync(); //! checks is given filename is a wallet; Q_INVOKABLE bool walletExists(const QString &path) const; @@ -125,6 +125,8 @@ private: explicit WalletManager(QObject *parent = 0); static WalletManager * m_instance; Monero::WalletManager * m_pimpl; + QMutex m_mutex; + QPointer m_currentWallet; };