From c570e74384a63b88d38d7697be435dd8de130ac8 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Thu, 31 Oct 2019 15:01:29 +0000 Subject: [HICN-371] Solve memory leaks in libtransport. Signed-off-by: Mauro Sardara Change-Id: I654843c3361c7bdb4b160f2441f08e8d91e97384 Signed-off-by: Mauro Sardara --- .../transport/core/hicn_forwarder_interface.cc | 2 +- libtransport/src/hicn/transport/core/portal.h | 24 ++++++++++++++-------- .../src/hicn/transport/utils/object_pool.h | 9 +++++++- utils/src/hiperf.cc | 5 +++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc b/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc index 33a37f540..7047a3a67 100644 --- a/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc +++ b/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc @@ -63,7 +63,7 @@ void fillCommandHeader(CommandHeader *header) { RouteToSelfCommand createCommandRoute(std::unique_ptr &&addr, uint8_t prefix_length) { - RouteToSelfCommand command; + RouteToSelfCommand command = {0}; // check and set IP address if (addr->sa_family == AF_INET) { diff --git a/libtransport/src/hicn/transport/core/portal.h b/libtransport/src/hicn/transport/core/portal.h index 5796308b4..b2614d361 100644 --- a/libtransport/src/hicn/transport/core/portal.h +++ b/libtransport/src/hicn/transport/core/portal.h @@ -312,10 +312,10 @@ class Portal { Portal(asio::io_service &io_service) : io_service_(io_service), + packet_pool_(io_service), app_name_("libtransport_application"), consumer_callback_(nullptr), producer_callback_(nullptr), - packet_pool_(io_service), connector_(std::bind(&Portal::processIncomingMessages, this, std::placeholders::_1), std::bind(&Portal::setLocalRoutes, this), io_service_, @@ -419,10 +419,13 @@ class Portal { this, std::placeholders::_1, hash))); auto it = pending_interest_hash_table_.find(hash); - if(it != pending_interest_hash_table_.end()){ + if (it != pending_interest_hash_table_.end()) { it->second->cancelTimer(); + + // Get reference to interest packet in order to have it destroyed. + auto _int = it->second->getInterest(); it->second = std::move(pending_interest); - }else{ + } else { pending_interest_hash_table_[hash] = std::move(pending_interest); } } @@ -562,6 +565,10 @@ class Portal { TRANSPORT_ALWAYS_INLINE void doClear() { for (auto &pend_interest : pending_interest_hash_table_) { pend_interest.second->cancelTimer(); + + // Get interest packet from pending interest and do nothing with it. It + // will get destroyed as it goes out of scope. + auto _int = pend_interest.second->getInterest(); } pending_interest_hash_table_.clear(); @@ -641,13 +648,14 @@ class Portal { PendingInterest::Ptr interest_ptr = std::move(it->second); pending_interest_hash_table_.erase(it); interest_ptr->cancelTimer(); + auto _int = interest_ptr->getInterest(); if (interest_ptr->getOnDataCallback() != UNSET_CALLBACK) { - interest_ptr->on_content_object_callback_( - std::move(interest_ptr->getInterest()), std::move(content_object)); + interest_ptr->on_content_object_callback_(std::move(_int), + std::move(content_object)); } else if (consumer_callback_) { - consumer_callback_->onContentObject( - std::move(interest_ptr->getInterest()), std::move(content_object)); + consumer_callback_->onContentObject(std::move(_int), + std::move(content_object)); } } else { TRANSPORT_LOGD("No pending interests for current content (%s)", @@ -668,6 +676,7 @@ class Portal { private: asio::io_service &io_service_; asio::io_service internal_io_service_; + portal_details::Pool packet_pool_; std::string app_name_; @@ -677,7 +686,6 @@ class Portal { ConsumerCallback *consumer_callback_; ProducerCallback *producer_callback_; - portal_details::Pool packet_pool_; portal_details::HandlerMemory async_callback_memory_; typename ForwarderInt::ConnectorType connector_; diff --git a/libtransport/src/hicn/transport/utils/object_pool.h b/libtransport/src/hicn/transport/utils/object_pool.h index e34730e81..e8f837753 100644 --- a/libtransport/src/hicn/transport/utils/object_pool.h +++ b/libtransport/src/hicn/transport/utils/object_pool.h @@ -47,7 +47,12 @@ class ObjectPool { ObjectPool() : destructor_(false) {} - ~ObjectPool() { destructor_ = true; } + ~ObjectPool() { + destructor_ = true; + for (auto &ptr : object_pool_) { + ptr.reset(); + } + } std::pair get() { if (object_pool_.empty()) { @@ -65,6 +70,8 @@ class ObjectPool { if (TRANSPORT_EXPECT_TRUE(!destructor_)) { object_pool_.emplace_back(makePtr(object)); + } else { + delete object; } } diff --git a/utils/src/hiperf.cc b/utils/src/hiperf.cc index 94e5d998d..078e9c3f1 100644 --- a/utils/src/hiperf.cc +++ b/utils/src/hiperf.cc @@ -191,6 +191,11 @@ class HIperfClient { rtc_callback_(configuration_.rtc_ ? new RTCCallback(*this) : nullptr), callback_(configuration_.rtc_ ? nullptr : new Callback(*this)) {} + ~HIperfClient() { + delete callback_; + delete rtc_callback_; + } + void checkReceivedRtcContent(ConsumerSocket &c, const ContentObject &contentObject) { if (!configuration_.test_mode_) return; -- cgit 1.2.3-korg