diff options
-rw-r--r-- | comphelper/source/misc/asyncnotification.cxx | 5 | ||||
-rw-r--r-- | salhelper/source/thread.cxx | 15 |
2 files changed, 13 insertions, 7 deletions
diff --git a/comphelper/source/misc/asyncnotification.cxx b/comphelper/source/misc/asyncnotification.cxx index e2c5691899d3..81552d9750cf 100644 --- a/comphelper/source/misc/asyncnotification.cxx +++ b/comphelper/source/misc/asyncnotification.cxx @@ -18,6 +18,7 @@ */ #include <comphelper/asyncnotification.hxx> +#include <comphelper/scopeguard.hxx> #include <mutex> #include <condition_variable> #include <osl/mutex.hxx> @@ -231,16 +232,20 @@ namespace comphelper { // see salhelper::Thread::launch xThis->m_xImpl->pKeepThisAlive = xThis; + comphelper::ScopeGuard g([&xThis] { xThis->m_xImpl->pKeepThisAlive.reset(); }); if (!xThis->create()) { throw std::runtime_error("osl::Thread::create failed"); } + g.dismiss(); } void AsyncEventNotifierAutoJoin::run() { // see salhelper::Thread::run + comphelper::ScopeGuard g([this] { onTerminated(); }); setName(m_xImpl->name); execute(); + g.dismiss(); } void AsyncEventNotifierAutoJoin::onTerminated() diff --git a/salhelper/source/thread.cxx b/salhelper/source/thread.cxx index 190daf5081b6..66713a2a3f10 100644 --- a/salhelper/source/thread.cxx +++ b/salhelper/source/thread.cxx @@ -12,6 +12,7 @@ #include <stdexcept> #include <string> +#include <comphelper/scopeguard.hxx> #include <sal/log.hxx> #include <salhelper/thread.hxx> @@ -22,21 +23,21 @@ void salhelper::Thread::launch() { // Assumption is that osl::Thread::create returns normally with a true // return value iff it causes osl::Thread::run to start executing: acquire(); - try { - if (!create()) { - throw std::runtime_error("osl::Thread::create failed"); - } - } catch (...) { - release(); - throw; + comphelper::ScopeGuard g([this] { release(); }); + if (!create()) { + throw std::runtime_error("osl::Thread::create failed"); } + g.dismiss(); } salhelper::Thread::~Thread() {} void salhelper::Thread::run() { + // Work around the problem that onTerminated is not called if run throws an exception: + comphelper::ScopeGuard g([this] { onTerminated(); }); setName(name_); execute(); + g.dismiss(); } void salhelper::Thread::onTerminated() { release(); } |