From 109cc3f232f0f73eb59df50b37e192a41f33e5ce Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 11 Apr 2007 17:22:19 +0000 Subject: [PATCH] made the announce timer (for dht and lsd) use a weak pointer instead of raw pointer, to avoid problems when torrent is being destructed --- include/libtorrent/torrent.hpp | 5 ++++- src/torrent.cpp | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 720c86ac1..d59492eee 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -571,8 +571,11 @@ namespace libtorrent // by the DHT. deadline_timer m_announce_timer; + static void on_announce_disp(boost::weak_ptr p + , asio::error_code const& e); + // this is called once per announce interval - void on_announce(asio::error_code const& e); + void on_announce(); #ifndef TORRENT_DISABLE_DHT static void on_dht_announce_response_disp(boost::weak_ptr t diff --git a/src/torrent.cpp b/src/torrent.cpp index e4b2c9022..4a5ba7127 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -256,9 +256,10 @@ namespace libtorrent m_policy.reset(new policy(this)); init(); + boost::weak_ptr self(shared_from_this()); m_announce_timer.expires_from_now(seconds(1)); m_announce_timer.async_wait(m_ses.m_strand.wrap( - bind(&torrent::on_announce, this, _1))); + bind(&torrent::on_announce_disp, self, _1))); } torrent::torrent( @@ -342,9 +343,11 @@ namespace libtorrent } m_policy.reset(new policy(this)); + + boost::weak_ptr self(shared_from_this()); m_announce_timer.expires_from_now(seconds(1)); m_announce_timer.async_wait(m_ses.m_strand.wrap( - bind(&torrent::on_announce, this, _1))); + bind(&torrent::on_announce_disp, self, _1))); } #ifndef TORRENT_DISABLE_DHT @@ -422,13 +425,22 @@ namespace libtorrent m_net_interface = tcp::endpoint(address::from_string(net_interface), 0); } - void torrent::on_announce(asio::error_code const& e) + void torrent::on_announce_disp(boost::weak_ptr p + , asio::error_code const& e) { if (e) return; + boost::shared_ptr t = p.lock(); + if (!t) return; + t->on_announce(); + } + + void torrent::on_announce() + { + boost::weak_ptr self(shared_from_this()); m_announce_timer.expires_from_now(minutes(30)); m_announce_timer.async_wait(m_ses.m_strand.wrap( - bind(&torrent::on_announce, this, _1))); + bind(&torrent::on_announce_disp, self, _1))); // announce with the local discovery service m_ses.announce_lsd(m_torrent_file.info_hash()); @@ -439,7 +451,6 @@ namespace libtorrent { // 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()); assert(m_ses.m_external_listen_port > 0); m_ses.m_dht->announce(m_torrent_file.info_hash() , m_ses.m_external_listen_port @@ -2115,9 +2126,10 @@ namespace libtorrent // only start the announce if we want to announce with the dht if (should_announce_dht()) { + boost::weak_ptr self(shared_from_this()); m_announce_timer.expires_from_now(seconds(1)); m_announce_timer.async_wait(m_ses.m_strand.wrap( - bind(&torrent::on_announce, this, _1))); + bind(&torrent::on_announce_disp, self, _1))); } #endif