From f2f8eea2c22d30ff1b44b6960794de1548f99ceb Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 20 Aug 2021 17:11:54 -0600 Subject: [PATCH] Do not store unnecessary variables. --- include/deal.II/base/exceptions.h | 10 ++------- source/base/exceptions.cc | 36 +++++++++++-------------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/include/deal.II/base/exceptions.h b/include/deal.II/base/exceptions.h index a30b345155..9dd446d257 100644 --- a/include/deal.II/base/exceptions.h +++ b/include/deal.II/base/exceptions.h @@ -61,7 +61,7 @@ public: /** * Destructor. */ - virtual ~ExceptionBase() noexcept override; + virtual ~ExceptionBase() noexcept override = default; /** * Copy operator. This operator is deleted since exception objects @@ -142,13 +142,7 @@ protected: const char *exc; /** - * A backtrace to the position where the problem happened, if the system - * supports this. - */ - mutable char **stacktrace; - - /** - * The number of stacktrace frames that are stored in the previous variable. + * The number of stacktrace frames that are stored in the following variable. * Zero if the system does not support stack traces. */ int n_stacktrace_frames; diff --git a/source/base/exceptions.cc b/source/base/exceptions.cc index b04ef35882..37c85104b9 100644 --- a/source/base/exceptions.cc +++ b/source/base/exceptions.cc @@ -92,7 +92,6 @@ ExceptionBase::ExceptionBase() , function("") , cond("") , exc("") - , stacktrace(nullptr) , n_stacktrace_frames(0) , what_str("") { @@ -109,7 +108,6 @@ ExceptionBase::ExceptionBase(const ExceptionBase &exc) , function(exc.function) , cond(exc.cond) , exc(exc.exc) - , stacktrace(nullptr) , n_stacktrace_frames(exc.n_stacktrace_frames) , what_str("") // don't copy the error message, it gets generated dynamically // by what() @@ -126,14 +124,6 @@ ExceptionBase::ExceptionBase(const ExceptionBase &exc) -ExceptionBase::~ExceptionBase() noexcept -{ - free(stacktrace); // free(nullptr) is allowed - stacktrace = nullptr; -} - - - void ExceptionBase::set_fields(const char *f, const int l, @@ -164,19 +154,7 @@ ExceptionBase::what() const noexcept { // If no error c_string was generated so far, do it now: if (what_str.empty()) - { -#ifdef DEAL_II_HAVE_GLIBC_STACKTRACE - // We have deferred the symbol lookup to this point to avoid costly - // runtime penalties due to linkage of external libraries by - // backtrace_symbols. - - // first delete old stacktrace if necessary - free(stacktrace); // free(nullptr) is allowed - stacktrace = backtrace_symbols(raw_stacktrace, n_stacktrace_frames); -#endif - - generate_message(); - } + generate_message(); return what_str.c_str(); } @@ -240,6 +218,15 @@ ExceptionBase::print_stack_trace(std::ostream &out) const if (deal_II_exceptions::internals::show_stacktrace == false) return; + char **stacktrace = nullptr; +#ifdef DEAL_II_HAVE_GLIBC_STACKTRACE + // We have deferred the symbol lookup to this point to avoid costly + // runtime penalties due to linkage of external libraries by + // backtrace_symbols. + stacktrace = backtrace_symbols(raw_stacktrace, n_stacktrace_frames); +#endif + + // if there is a stackframe stored, print it out << std::endl; out << "Stacktrace:" << std::endl << "-----------" << std::endl; @@ -316,6 +303,9 @@ ExceptionBase::print_stack_trace(std::ostream &out) const if (functionname == "main") break; } + + free(stacktrace); // free(nullptr) is allowed + stacktrace = nullptr; } -- 2.39.5