From 3783ff6420c781f386792dea25f62f823da7f8f5 Mon Sep 17 00:00:00 2001 From: Joachim Klein Date: Mon, 20 Nov 2017 10:58:17 +0100 Subject: [PATCH] Fix memory leak in BaseException (and derived exceptions) --- src/storm/exceptions/BaseException.cpp | 7 ++----- src/storm/exceptions/BaseException.h | 4 ++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/storm/exceptions/BaseException.cpp b/src/storm/exceptions/BaseException.cpp index 626231487..86119f600 100644 --- a/src/storm/exceptions/BaseException.cpp +++ b/src/storm/exceptions/BaseException.cpp @@ -19,11 +19,8 @@ namespace storm { } const char* BaseException::what() const NOEXCEPT { - std::string errorString = this->stream.str(); - char* result = new char[errorString.size() + 1]; - result[errorString.size()] = '\0'; - std::copy(errorString.begin(), errorString.end(), result); - return result; + errorString = this->stream.str(); + return errorString.c_str(); } } } diff --git a/src/storm/exceptions/BaseException.h b/src/storm/exceptions/BaseException.h index f9224cfb9..a3febc290 100644 --- a/src/storm/exceptions/BaseException.h +++ b/src/storm/exceptions/BaseException.h @@ -46,6 +46,10 @@ namespace storm { protected: // This stream stores the message of this exception. std::stringstream stream; + + private: + // storage for the string backing the C string returned by what() + mutable std::string errorString; }; } // namespace exceptions