fix memory leak using shared_ptr references

This commit is contained in:
woodser
2019-07-08 16:00:18 -04:00
parent 74b5cbe708
commit cd57cab71f
3 changed files with 11 additions and 8 deletions
+8 -5
View File
@@ -63,7 +63,7 @@ namespace monero {
// ----------------------- UNDECLARED PRIVATE HELPERS -----------------------
void mergeTx(vector<shared_ptr<MoneroTx>> txs, const shared_ptr<MoneroTx>& tx) {
void mergeTx(vector<shared_ptr<MoneroTx>>& txs, const shared_ptr<MoneroTx>& tx) {
for (const shared_ptr<MoneroTx>& 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<MoneroOutput> merger : other->vins) {
for (const shared_ptr<MoneroOutput>& merger : other->vins) {
bool merged = false;
merger->tx = self;
for (const shared_ptr<MoneroOutput>& mergee : vins) {
@@ -293,7 +296,7 @@ namespace monero {
// merge vouts
if (!other->vouts.empty()) {
for (shared_ptr<MoneroOutput> vout : other->vouts) vout->tx = self;
for (const shared_ptr<MoneroOutput>& 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<MoneroOutput> merger : other->vouts) {
for (const shared_ptr<MoneroOutput>& merger : other->vouts) {
bool merged = false;
merger->tx = self; // TODO: this update needs made in Java and JS
for (const shared_ptr<MoneroOutput>& mergee : vouts) {
+1 -1
View File
@@ -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<MoneroTxWallet> tx, map<string, shared_ptr<MoneroTxWallet>>& txMap, map<uint64_t, shared_ptr<MoneroBlock>>& blockMap, bool skipIfAbsent) {
void mergeTx(const shared_ptr<MoneroTxWallet>& tx, map<string, shared_ptr<MoneroTxWallet>>& txMap, map<uint64_t, shared_ptr<MoneroBlock>>& 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)
+2 -2
View File
@@ -67,7 +67,7 @@ namespace monero {
// ----------------------- UNDECLARED PRIVATE HELPERS -----------------------
void mergeIncomingTransfer(vector<shared_ptr<MoneroIncomingTransfer>> transfers, const shared_ptr<MoneroIncomingTransfer>& transfer) {
void mergeIncomingTransfer(vector<shared_ptr<MoneroIncomingTransfer>>& transfers, const shared_ptr<MoneroIncomingTransfer>& transfer) {
for (const shared_ptr<MoneroIncomingTransfer>& 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<MoneroIncomingTransfer> transfer : other->incomingTransfers) { // NOTE: not using reference so shared_ptr is not deleted when tx is dereferenced
for (const shared_ptr<MoneroIncomingTransfer>& transfer : other->incomingTransfers) { // NOTE: not using reference so shared_ptr is not deleted when tx is dereferenced
transfer->tx = self;
mergeIncomingTransfer(self->incomingTransfers, transfer);
}