From cd57cab71f8a820c128adb26738a041abd6b7fe7 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 8 Jul 2019 16:00:18 -0400 Subject: [PATCH] fix memory leak using shared_ptr references --- src/daemon/MoneroDaemonModel.cpp | 13 ++++++++----- src/wallet/MoneroWallet.cpp | 2 +- src/wallet/MoneroWalletModel.cpp | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/daemon/MoneroDaemonModel.cpp b/src/daemon/MoneroDaemonModel.cpp index 6cd757b..63ff457 100644 --- a/src/daemon/MoneroDaemonModel.cpp +++ b/src/daemon/MoneroDaemonModel.cpp @@ -63,7 +63,7 @@ namespace monero { // ----------------------- UNDECLARED PRIVATE HELPERS ----------------------- - void mergeTx(vector> txs, const shared_ptr& tx) { + void mergeTx(vector>& txs, const shared_ptr& tx) { for (const shared_ptr& aTx : txs) { if (aTx->id.get() == tx->id.get()) { aTx->merge(aTx, tx); @@ -161,7 +161,10 @@ namespace monero { // merge coinbase tx if (coinbaseTx == boost::none) coinbaseTx = other->coinbaseTx; - else if (other->coinbaseTx != boost::none) coinbaseTx.get()->merge(*coinbaseTx, *other->coinbaseTx); + else if (other->coinbaseTx != boost::none) { + other->coinbaseTx.get()->block = self; + coinbaseTx.get()->merge(*coinbaseTx, *other->coinbaseTx); + } // merge non-coinbase txs if (!other->txs.empty()) { @@ -277,7 +280,7 @@ namespace monero { // merge vins if (!other->vins.empty()) { - for (shared_ptr merger : other->vins) { + for (const shared_ptr& merger : other->vins) { bool merged = false; merger->tx = self; for (const shared_ptr& mergee : vins) { @@ -293,7 +296,7 @@ namespace monero { // merge vouts if (!other->vouts.empty()) { - for (shared_ptr vout : other->vouts) vout->tx = self; + for (const shared_ptr& vout : other->vouts) vout->tx = self; if (vouts.empty()) vouts = other->vouts; else { @@ -315,7 +318,7 @@ namespace monero { // merge by key images if (numKeyImages > 0) { - for (shared_ptr merger : other->vouts) { + for (const shared_ptr& merger : other->vouts) { bool merged = false; merger->tx = self; // TODO: this update needs made in Java and JS for (const shared_ptr& mergee : vouts) { diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index d8080c3..e3f7bed 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -351,7 +351,7 @@ namespace monero { * @param blockMap maps block heights to blocks * @param skipIfAbsent specifies if the tx should not be added if it doesn't already exist */ - void mergeTx(shared_ptr tx, map>& txMap, map>& blockMap, bool skipIfAbsent) { + void mergeTx(const shared_ptr& tx, map>& txMap, map>& blockMap, bool skipIfAbsent) { if (tx->id == boost::none) throw runtime_error("Tx id is not initialized"); // if tx doesn't exist, add it (unless skipped) diff --git a/src/wallet/MoneroWalletModel.cpp b/src/wallet/MoneroWalletModel.cpp index 465e58a..004d725 100644 --- a/src/wallet/MoneroWalletModel.cpp +++ b/src/wallet/MoneroWalletModel.cpp @@ -67,7 +67,7 @@ namespace monero { // ----------------------- UNDECLARED PRIVATE HELPERS ----------------------- - void mergeIncomingTransfer(vector> transfers, const shared_ptr& transfer) { + void mergeIncomingTransfer(vector>& transfers, const shared_ptr& transfer) { for (const shared_ptr& aTransfer : transfers) { if (aTransfer->accountIndex.get() == transfer->accountIndex.get() && aTransfer->subaddressIndex.get() == transfer->subaddressIndex.get()) { aTransfer->merge(aTransfer, transfer); @@ -143,7 +143,7 @@ namespace monero { // merge incoming transfers if (!other->incomingTransfers.empty()) { - for (const shared_ptr transfer : other->incomingTransfers) { // NOTE: not using reference so shared_ptr is not deleted when tx is dereferenced + for (const shared_ptr& transfer : other->incomingTransfers) { // NOTE: not using reference so shared_ptr is not deleted when tx is dereferenced transfer->tx = self; mergeIncomingTransfer(self->incomingTransfers, transfer); }