From b2331969d523d8a1ae2fe14e5c2f74600a7ab1a2 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 28 Sep 2011 18:31:05 +0200 Subject: [PATCH] Fix a crash on exit when using curl The problem was that the callback thread is destroyed only in the destructor of the callback_base class (which is called after the destructor of the derived classes). This means that the thread is running even when it's object is partly destroyed, which can cause segfaults, race conditions and other nasty problems. I've fixed it so that the thread is destroyed before the underlying object's destructor is called. --- src/update-cb.cc | 14 ++++++++++++-- src/update-cb.hh | 9 ++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/update-cb.cc b/src/update-cb.cc index ad8b2d63..1cd19e38 100644 --- a/src/update-cb.cc +++ b/src/update-cb.cc @@ -35,6 +35,11 @@ namespace conky { namespace priv { callback_base::~callback_base() + { + stop(); + } + + void callback_base::stop() { if(thread) { done = true; @@ -43,11 +48,16 @@ namespace conky { write(pipefd.second, "X", 1); thread->join(); delete thread; + thread = NULL; } - if(pipefd.first >= 0) + if(pipefd.first >= 0) { close(pipefd.first); - if(pipefd.second >= 0) + pipefd.first = -1; + } + if(pipefd.second >= 0) { close(pipefd.second); + pipefd.second = -1; + } } inline size_t callback_base::get_hash(const handle &h) diff --git a/src/update-cb.hh b/src/update-cb.hh index fb98634f..470d38c3 100644 --- a/src/update-cb.hh +++ b/src/update-cb.hh @@ -68,6 +68,13 @@ namespace conky { void run(); void start_routine(); + void stop(); + + static void deleter(callback_base *ptr) + { + ptr->stop(); + delete ptr; + } // a list of registered callbacks static Callbacks callbacks; @@ -118,7 +125,7 @@ namespace conky { typedef std::shared_ptr Base; callback_handle(Callback *ptr) - : Base(ptr) + : Base(ptr, &priv::callback_base::deleter) {} callback_handle(Base &&ptr)