From ff4d9aa0df32285a357dee498867c77e5b0adf2c Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Mar 2004 02:29:55 +0000 Subject: [PATCH] Don't delete the waiter until we have finished accessing the dispatcher implementation, just in case the waiter's handler owns the dispatcher. --- .../detail/locking_dispatcher_service.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/asio/include/asio/detail/locking_dispatcher_service.hpp b/asio/include/asio/detail/locking_dispatcher_service.hpp index 511479a4..cf2aeaee 100644 --- a/asio/include/asio/detail/locking_dispatcher_service.hpp +++ b/asio/include/asio/detail/locking_dispatcher_service.hpp @@ -141,18 +141,35 @@ public: void operator()() { do_upcall(); + detail::mutex::scoped_lock lock(impl_.mutex_); + waiter_base* tmp = impl_.first_waiter_; impl_.first_waiter_ = impl_.first_waiter_->next_; - delete tmp; if (impl_.first_waiter_) { lock.unlock(); + + // Ensure the waiter is not deleted until after we have finished + // accessing the dispatcher, since the waiter might indirectly own + // the dispatcher and so destroying the waiter will cause the + // dispatcher to be destroyed. + delete tmp; + + // There is more work to do, so post this handler again. demuxer_.post(*this); } else { impl_.last_waiter_ = 0; + + lock.unlock(); + + // Ensure the waiter is not deleted until after we have finished + // accessing the dispatcher, since the waiter might indirectly own + // the dispatcher and so destroying the waiter will cause the + // dispatcher to be destroyed. + delete tmp; } }