From d0a38c50c615af4cce8f48f93e217f62e5645e62 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 29 Aug 2006 01:15:24 +0000 Subject: [PATCH] fixed crash bug occuring when removing a torrent while it announces on the DHT. Fixed the mainloop not to quit when an uncought exception is thrown. --- include/libtorrent/torrent.hpp | 2 ++ src/kademlia/closest_nodes.cpp | 1 - src/session.cpp | 28 +++++++++++++++------------- src/torrent.cpp | 16 ++++++++++++---- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 4c5b0126c..693e62326 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -495,6 +495,8 @@ namespace libtorrent tcp::resolver m_host_resolver; #ifndef TORRENT_DISABLE_DHT + static void on_dht_announce_response_disp(boost::weak_ptr t + , std::vector const& peers); deadline_timer m_dht_announce_timer; void on_dht_announce(asio::error const& e); void on_dht_announce_response(std::vector const& peers); diff --git a/src/kademlia/closest_nodes.cpp b/src/kademlia/closest_nodes.cpp index cc83e4fd1..8d0ccea87 100644 --- a/src/kademlia/closest_nodes.cpp +++ b/src/kademlia/closest_nodes.cpp @@ -118,7 +118,6 @@ void closest_nodes::invoke(node_id const& id, udp::endpoint addr) void closest_nodes::done() { - std::cerr << "[closest_nodes] DONE" << std::endl; std::vector results; int result_size = m_table.bucket_size(); if (result_size > (int)m_results.size()) result_size = (int)m_results.size(); diff --git a/src/session.cpp b/src/session.cpp index b281c67c3..5aa94d3b5 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -948,21 +948,23 @@ namespace libtorrent { namespace detail boost::posix_time::ptime timer = second_clock::universal_time(); - // for(;;) - // { - try + do { - m_selector.run(); - assert(m_abort == true); - } - catch (std::exception& e) - { -#ifndef NDEBUG - std::cerr << e.what() << "\n"; - std::string err = e.what(); -#endif - assert(false); + try + { + m_selector.run(); + assert(m_abort == true); + } + catch (std::exception& e) + { + #ifndef NDEBUG + std::cerr << e.what() << "\n"; + std::string err = e.what(); + #endif + assert(false); + } } + while (!m_abort); deadline_timer tracker_timer(m_selector); diff --git a/src/torrent.cpp b/src/torrent.cpp index 3dfda8218..5519cb2be 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -185,7 +185,6 @@ namespace peer_id const& pid; }; - } namespace libtorrent @@ -439,17 +438,26 @@ namespace libtorrent #ifndef TORRENT_DISABLE_DHT + void torrent::on_dht_announce_response_disp(boost::weak_ptr t + , std::vector const& peers) + { + boost::shared_ptr tor = t.lock(); + if (!tor) return; + tor->on_dht_announce_response(peers); + } + void torrent::on_dht_announce(asio::error const& e) { if (e) return; m_dht_announce_timer.expires_from_now(boost::posix_time::minutes(30)); m_dht_announce_timer.async_wait(bind(&torrent::on_dht_announce, this, _1)); if (!m_ses.m_dht) return; - // TODO: BUG! This may invoke on_dht_announce() with an invalid this-pointer. - // there has to be a way to abort an announce operation on the dht. + // TODO: There should be a way to abort an announce operation on the dht. + // when the torrent is destructed + boost::weak_ptr self(shared_from_this()); m_ses.m_dht->announce(m_torrent_file.info_hash() , m_ses.m_listen_interface.port() - , bind(&torrent::on_dht_announce_response, this, _1)); + , bind(&torrent::on_dht_announce_response_disp, self, _1)); } void torrent::on_dht_announce_response(std::vector const& peers)