ci: enable leak sanitizer

src: fix memory leaks caused by segment fetcher

refs: #4682

Change-Id: I4fdf24f369e2d27845f1f6e6ca9978905110a024
diff --git a/.jenkins.d/20-tests.sh b/.jenkins.d/20-tests.sh
index f3208b3..b172b2a 100755
--- a/.jenkins.d/20-tests.sh
+++ b/.jenkins.d/20-tests.sh
@@ -10,7 +10,6 @@
 }
 
 ASAN_OPTIONS="color=always"
-ASAN_OPTIONS+=":detect_leaks=false" #4682
 ASAN_OPTIONS+=":detect_stack_use_after_return=true"
 ASAN_OPTIONS+=":check_initialization_order=true"
 ASAN_OPTIONS+=":strict_init_order=true"
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index eae4a43..b8cda8e 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -28,7 +28,6 @@
 #include "utility/name-helper.hpp"
 
 #include <ndn-cxx/security/signing-helpers.hpp>
-#include <ndn-cxx/util/segment-fetcher.hpp>
 
 namespace nlsr {
 
@@ -85,10 +84,17 @@
 {
 }
 
+Lsdb::~Lsdb()
+{
+  for (const auto& sp : m_fetchers) {
+    sp->stop();
+  }
+}
+
 void
 Lsdb::onFetchLsaError(uint32_t errorCode,
                       const std::string& msg,
-                      ndn::Name& interestName,
+                      const ndn::Name& interestName,
                       uint32_t retransmitNo,
                       const ndn::time::steady_clock::TimePoint& deadline,
                       ndn::Name lsaName,
@@ -118,7 +124,7 @@
 }
 
 void
-Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
+Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
 {
   std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
   data->setContent(bufferPtr);
@@ -999,15 +1005,26 @@
   }
 
   ndn::Interest interest(interestName);
-  interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
+  ndn::util::SegmentFetcher::Options options;
+  options.interestLifetime = m_nlsr.getConfParameter().getLsaInterestLifetime();
 
   NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
   auto fetcher = ndn::util::SegmentFetcher::start(m_nlsr.getNlsrFace(),
-                                                  interest, m_nlsr.getValidator());
+                                                  interest, m_nlsr.getValidator(), options);
 
-  fetcher->onComplete.connect(std::bind(&Lsdb::afterFetchLsa, this, _1, interestName));
-  fetcher->onError.connect(std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
-                                     timeoutCount, deadline, lsaName, seqNo));
+  auto it = m_fetchers.insert(fetcher).first;
+
+  fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
+                                afterFetchLsa(bufferPtr, interestName);
+                                m_fetchers.erase(it);
+                              });
+
+  fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, &fetcher, it]
+                           (uint32_t errorCode, const std::string& msg) {
+                             onFetchLsaError(errorCode, msg, interestName,
+                                             timeoutCount, deadline, lsaName, seqNo);
+                             m_fetchers.erase(it);
+                           });
 
   m_lsaStorage.connectToFetcher(*fetcher);
   m_nlsr.connectToFetcher(*fetcher);
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index f3eb899..2ebb952 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -33,6 +33,8 @@
 #include <ndn-cxx/security/key-chain.hpp>
 #include <ndn-cxx/util/signal.hpp>
 #include <ndn-cxx/util/time.hpp>
+#include <ndn-cxx/util/segment-fetcher.hpp>
+
 #include <utility>
 #include <boost/cstdint.hpp>
 
@@ -45,6 +47,8 @@
 public:
   Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler);
 
+  ~Lsdb();
+
   SyncLogicHandler&
   getSyncLogicHandler()
   {
@@ -356,7 +360,7 @@
   void
   onFetchLsaError(uint32_t errorCode,
                   const std::string& msg,
-                  ndn::Name& interestName,
+                  const ndn::Name& interestName,
                   uint32_t retransmitNo,
                   const ndn::time::steady_clock::TimePoint& deadline,
                   ndn::Name lsaName,
@@ -369,7 +373,7 @@
             /<network>/NLSR/LSA/<site>/%C1.Router/<router>/<lsa-type>/<seqNo>
    */
   void
-  afterFetchLsa(const ndn::ConstBufferPtr& data, ndn::Name& interestName);
+  afterFetchLsa(const ndn::ConstBufferPtr& data, const ndn::Name& interestName);
 
 private:
   ndn::time::system_clock::TimePoint
@@ -411,6 +415,8 @@
   SequencingManager m_sequencingManager;
 
   ndn::util::signal::ScopedConnection m_onNewLsaConnection;
+
+  std::set<std::shared_ptr<ndn::util::SegmentFetcher>> m_fetchers;
 };
 
 } // namespace nlsr
diff --git a/src/route/fib.hpp b/src/route/fib.hpp
index 27e5967..32c656d 100644
--- a/src/route/fib.hpp
+++ b/src/route/fib.hpp
@@ -63,9 +63,6 @@
   {
   }
 
-  VIRTUAL_WITH_TESTS
-  ~Fib() = default;
-
   /*! \brief Completely remove a name prefix from the FIB.
    *
    * If a name prefix is found to no longer be reachable from this
@@ -74,7 +71,7 @@
    *
    * \sa nlsr::NamePrefixTable::removeEntry
    */
-  VIRTUAL_WITH_TESTS void
+  void
   remove(const ndn::Name& name);
 
   /*! \brief Set the nexthop list of a name.
@@ -88,7 +85,7 @@
    * \param name The name prefix that the next-hops apply to
    * \param allHops A complete list of next-hops to associate with name.
    */
-  VIRTUAL_WITH_TESTS void
+  void
   update(const ndn::Name& name, const NexthopList& allHops);
 
   /*! \brief Remove all entries from the FIB.