Skip to content

Commit

Permalink
Do not store unnecessary variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
bangerth committed Aug 21, 2021
1 parent 208d705 commit f2f8eea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
10 changes: 2 additions & 8 deletions include/deal.II/base/exceptions.h
Expand Up @@ -61,7 +61,7 @@ class ExceptionBase : public std::exception
/**
* Destructor.
*/
virtual ~ExceptionBase() noexcept override;
virtual ~ExceptionBase() noexcept override = default;

/**
* Copy operator. This operator is deleted since exception objects
Expand Down Expand Up @@ -142,13 +142,7 @@ class ExceptionBase : public std::exception
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;
Expand Down
36 changes: 13 additions & 23 deletions source/base/exceptions.cc
Expand Up @@ -92,7 +92,6 @@ ExceptionBase::ExceptionBase()
, function("")
, cond("")
, exc("")
, stacktrace(nullptr)
, n_stacktrace_frames(0)
, what_str("")
{
Expand All @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -316,6 +303,9 @@ ExceptionBase::print_stack_trace(std::ostream &out) const
if (functionname == "main")
break;
}

free(stacktrace); // free(nullptr) is allowed
stacktrace = nullptr;
}


Expand Down

0 comments on commit f2f8eea

Please sign in to comment.