From 0a937bb5b936dbfb67fd4a6629d33bbd23ecebb9 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 6 Oct 2018 18:07:56 -0700 Subject: [PATCH] wpiutil: SafeThread: Ensure thread is released in destructor (#1358) The thread must be released with either a detach or a join, otherwise std::terminate is called. --- wpiutil/src/main/native/cpp/SafeThread.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wpiutil/src/main/native/cpp/SafeThread.cpp b/wpiutil/src/main/native/cpp/SafeThread.cpp index 13ba1a4908..52ac70077e 100644 --- a/wpiutil/src/main/native/cpp/SafeThread.cpp +++ b/wpiutil/src/main/native/cpp/SafeThread.cpp @@ -40,9 +40,9 @@ void detail::SafeThreadOwnerBase::Stop() { if (auto thr = m_thread.lock()) { thr->m_active = false; thr->m_cond.notify_all(); - m_stdThread.detach(); m_thread.reset(); } + if (m_stdThread.joinable()) m_stdThread.detach(); } void detail::SafeThreadOwnerBase::Join() { @@ -54,6 +54,8 @@ void detail::SafeThreadOwnerBase::Join() { thr->m_active = false; thr->m_cond.notify_all(); stdThread.join(); + } else if (m_stdThread.joinable()) { + m_stdThread.detach(); } }