diff --git a/include/opendht/node_cache.h b/include/opendht/node_cache.h index 97e62cd1..9b1e2a06 100644 --- a/include/opendht/node_cache.h +++ b/include/opendht/node_cache.h @@ -37,12 +37,15 @@ struct NodeCache { */ void clearBadNodes(sa_family_t family = 0); + ~NodeCache(); + private: class NodeMap : public std::map> { public: Sp getNode(const InfoHash& id); Sp getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client); void clearBadNodes(); + void setExpired(); }; const NodeMap& cache(sa_family_t af) const { return af == AF_INET ? cache_4 : cache_6; } diff --git a/src/node_cache.cpp b/src/node_cache.cpp index 68b6441b..485b208b 100644 --- a/src/node_cache.cpp +++ b/src/node_cache.cpp @@ -20,6 +20,12 @@ namespace dht { +NodeCache::~NodeCache() +{ + cache_4.setExpired(); + cache_6.setExpired(); +} + Sp NodeCache::getNode(const InfoHash& id, sa_family_t family) { return cache(family).getNode(id); @@ -116,4 +122,12 @@ NodeCache::NodeMap::clearBadNodes() { } } +void +NodeCache::NodeMap::setExpired() { + for (auto& wn : *this) + if (auto n = wn.second.lock()) + n->setExpired(); + clear(); +} + }