diff --git a/libp2p/Common.h b/libp2p/Common.h index 9caa9c4c7..c1334f7a4 100644 --- a/libp2p/Common.h +++ b/libp2p/Common.h @@ -221,7 +221,7 @@ class DeadlineOps { public: DeadlineOp(ba::io_service& _io, unsigned _msInFuture, std::function const& _f): m_timer(new ba::deadline_timer(_io)) { m_timer->expires_from_now(boost::posix_time::milliseconds(_msInFuture)); m_timer->async_wait(_f); } - ~DeadlineOp() {} + ~DeadlineOp() { if (m_timer) m_timer->cancel(); } DeadlineOp(DeadlineOp&& _s): m_timer(_s.m_timer.release()) {} DeadlineOp& operator=(DeadlineOp&& _s) @@ -247,6 +247,8 @@ public: void schedule(unsigned _msInFuture, std::function const& _f) { if (m_stopped) return; DEV_GUARDED(x_timers) m_timers.emplace_back(m_io, _msInFuture, _f); } void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); } + + bool isStopped() const { return m_stopped; } protected: void reap(); diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 9395466f4..fad9e4958 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -67,8 +67,8 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint NodeTable::~NodeTable() { - m_timers.stop(); m_socketPointer->disconnect(); + m_timers.stop(); } void NodeTable::processEvents() @@ -199,7 +199,17 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr m_socket; ///< Shared pointer for our UDPSocket; ASIO requires shared_ptr. NodeSocket* m_socketPointer; ///< Set to m_socket.get(). Socket is created in constructor and disconnected in destructor to ensure access to pointer is safe. - DeadlineOps m_timers; + DeadlineOps m_timers; ///< this should be the last member - it must be destroyed first }; inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable)