From 3db950b1c9bab93b360104f62e2bb954d818b540 Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 30 Apr 2020 10:53:42 -0400 Subject: [PATCH] free memory in onOutputSpent/Received() with monero_utils::free(blocks) --- src/utils/monero_utils.h | 31 +++++++++++++++++++++++++++++++ src/wallet/monero_wallet_core.cpp | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/utils/monero_utils.h b/src/utils/monero_utils.h index bd762f8..9402c92 100644 --- a/src/utils/monero_utils.h +++ b/src/utils/monero_utils.h @@ -152,5 +152,36 @@ namespace monero_utils CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base"); return ss.str(); } + + /** + * Free memory of a block. + * + * @param block is the block to free + */ + static void free(shared_ptr block) { + for (shared_ptr& tx : block->m_txs) { + tx->m_block.reset(); + monero_tx_wallet* tx_wallet = dynamic_cast(tx.get()); + if (tx_wallet != nullptr) { + if (tx_wallet->m_outgoing_transfer != boost::none) tx_wallet->m_outgoing_transfer.get()->m_tx.reset(); + for (shared_ptr transfer : tx_wallet->m_incoming_transfers) transfer->m_tx.reset(); + for (shared_ptr output : tx_wallet->m_outputs) output->m_tx.reset(); + for (shared_ptr input : tx_wallet->m_inputs) { + input->m_key_image.reset(); + input->m_tx.reset(); + } + } + } + block.reset(); + } + + /** + * Free memory of blocks. + * + * @param blocks are blocks to free + */ + static void free(vector> blocks) { + for (shared_ptr& block : blocks) monero_utils::free(block); + } } #endif /* monero_utils_h */ diff --git a/src/wallet/monero_wallet_core.cpp b/src/wallet/monero_wallet_core.cpp index 085dd66..f7ce02e 100644 --- a/src/wallet/monero_wallet_core.cpp +++ b/src/wallet/monero_wallet_core.cpp @@ -731,6 +731,11 @@ namespace monero { for (monero_wallet_listener* listener : m_wallet.get_listeners()) { listener->on_output_received(*output); } + + // free memory + monero_utils::free(block); + output.reset(); + tx.reset(); } void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& cn_tx_in, uint64_t amount, const cryptonote::transaction& cn_tx_out, const cryptonote::subaddress_index& subaddr_index) { @@ -756,6 +761,11 @@ namespace monero { listener->on_output_spent(*output); } + // free memory + monero_utils::free(block); + output.reset(); + tx.reset(); + // TODO **: to notify or not to notify? // std::string tx_hash = epee::string_tools::pod_to_hex(txid); // LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height