From: Michal 'vorner' Vaner Date: Wed, 26 Sep 2012 09:29:07 +0000 (+0200) Subject: [2202] Don't throw from destructors X-Git-Tag: trac2402_base~79^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e3d1a58e4ec1857fc0264e8070ce67f569b7e42;p=thirdparty%2Fkea.git [2202] Don't throw from destructors The situations are very bad anyway, and that should not happen during normal operation. So we assert instead. --- diff --git a/src/lib/util/threads/lock.cc b/src/lib/util/threads/lock.cc index 7c34baa56d..d5c32d4179 100644 --- a/src/lib/util/threads/lock.cc +++ b/src/lib/util/threads/lock.cc @@ -47,11 +47,9 @@ struct Deinitializer { {} ~Deinitializer() { const int result = pthread_mutexattr_destroy(&attributes_); - if (result != 0) { - // This really should not happen. We might as well - // try to use assert here. - isc_throw(isc::InvalidOperation, strerror(result)); - } + // This should never happen. According to the man page, + // if there's error, it's our fault. + assert(result == 0); } pthread_mutexattr_t& attributes_; }; @@ -101,16 +99,17 @@ Mutex::~Mutex() { const int result = pthread_mutex_destroy(&impl_->mutex); const bool locked = impl_->locked_count != 0; delete impl_; - if (result != 0) { - // Yes, really throwing from the destructor. - // But the error should not happen during normal - // operations, this means something is screwed up - // and must be fixed. - isc_throw(isc::InvalidOperation, strerror(result)); - } - if (locked) { - isc_throw(isc::InvalidOperation, "Destroying locked mutex"); - } + // We don't want to throw from the destructor. Also, if this ever + // fails, something is really screwed up a lot. + assert(result == 0); + + // We should not try to destroy a locked mutex, bad threaded monsters + // could get loose if we ever do and it is also forbidden by pthreads. + + // This should not be possible to happen, since the + // pthread_mutex_destroy should check for it already. But it seems + // there are systems that don't check it. + assert(!locked); } } diff --git a/src/lib/util/threads/thread.cc b/src/lib/util/threads/thread.cc index d515403945..bbf7f513e2 100644 --- a/src/lib/util/threads/thread.cc +++ b/src/lib/util/threads/thread.cc @@ -113,13 +113,9 @@ Thread::~Thread() { const int result = pthread_detach(impl_->tid); Impl::done(impl_); impl_ = NULL; - if (result != 0) { - // Yes, really throwing from destructor. But this would - // mean someone really messed up the internal state, so - // we need to do something about it, even if it causes - // application to terminate. - isc_throw(isc::InvalidOperation, strerror(result)); - } + // If the detach ever fails, something is screwed rather + // badly. + assert(result == 0); } }