From 756d465ca9b3c157feebc464cee53e11d229b384 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 8 Mar 2013 14:41:01 +0100 Subject: [PATCH] Use TLS for last exception stack trace. --- lib/base/exception.cpp | 13 ++++++------- lib/base/exception.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/base/exception.cpp b/lib/base/exception.cpp index 810c336b7..f533d1636 100644 --- a/lib/base/exception.cpp +++ b/lib/base/exception.cpp @@ -21,7 +21,7 @@ using namespace icinga; -StackTrace *Exception::m_StackTrace = NULL; +boost::thread_specific_ptr Exception::m_LastStackTrace; /** * Retrieves the error code for the exception. @@ -146,11 +146,13 @@ void __cxa_throw(void *obj, void *pvtinfo, void (*dest)(void *)) if (tinfo->__is_pointer_p()) thrown_ptr = *(void **)thrown_ptr; + StackTrace trace; + Exception::SetLastStackTrace(trace); + /* Check if thrown_ptr inherits from boost::exception. */ if (boost_exc->__do_catch(tinfo, &thrown_ptr, 1)) { boost::exception *ex = (boost::exception *)thrown_ptr; - StackTrace trace; *ex << StackTraceErrorInfo(trace); } @@ -160,14 +162,11 @@ void __cxa_throw(void *obj, void *pvtinfo, void (*dest)(void *)) StackTrace *Exception::GetLastStackTrace(void) { - return m_StackTrace; + return m_LastStackTrace.get(); } void Exception::SetLastStackTrace(const StackTrace& trace) { - if (m_StackTrace) - delete m_StackTrace; - - m_StackTrace = new StackTrace(trace); + m_LastStackTrace.reset(new StackTrace(trace)); } diff --git a/lib/base/exception.h b/lib/base/exception.h index 8d7335796..8af7e83ca 100644 --- a/lib/base/exception.h +++ b/lib/base/exception.h @@ -65,7 +65,7 @@ private: String m_Message; int m_Code; - static StackTrace *m_StackTrace; + static boost::thread_specific_ptr m_LastStackTrace; }; typedef boost::error_info StackTraceErrorInfo;