From f9923bac95a45f213c5c727881f9d95f71a5f812 Mon Sep 17 00:00:00 2001 From: gereon Date: Tue, 22 Jan 2013 20:12:30 +0100 Subject: [PATCH] Fixed memory leaks involving Settings class Settings (being a singleton) will now free it's instance itself upon program termination. --- src/storm.cpp | 7 +----- src/utility/Settings.cpp | 2 ++ src/utility/Settings.h | 47 +++++++++++++++++++++++++++++++++++++--- test/storm-tests.cpp | 2 -- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/storm.cpp b/src/storm.cpp index 65c2b835a..250163f70 100644 --- a/src/storm.cpp +++ b/src/storm.cpp @@ -99,18 +99,15 @@ bool parseOptions(const int argc, const char* argv[]) { } catch (storm::exceptions::InvalidSettingsException& e) { std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl; std::cout << std::endl << storm::settings::help; - delete s; return false; } if (s->isSet("help")) { std::cout << storm::settings::help; - delete s; return false; } if (s->isSet("test-prctl")) { storm::parser::PrctlParser parser(s->getString("test-prctl").c_str()); - delete s; return false; } @@ -133,9 +130,7 @@ bool parseOptions(const int argc, const char* argv[]) { * Function to perform some cleanup. */ void cleanUp() { - if (storm::settings::instance() != nullptr) { - delete storm::settings::instance(); - } + // nothing here } void testCheckingDie(storm::models::Dtmc& dtmc) { diff --git a/src/utility/Settings.cpp b/src/utility/Settings.cpp index 675a9ae8e..26f733071 100644 --- a/src/utility/Settings.cpp +++ b/src/utility/Settings.cpp @@ -33,6 +33,8 @@ storm::settings::Settings* storm::settings::Settings::inst = nullptr; std::map< std::pair, std::shared_ptr > storm::settings::Settings::modules; +storm::settings::Destroyer storm::settings::Settings::destroyer; + /*! * The constructor fills the option descriptions, parses the * command line and the config file and puts the option values to diff --git a/src/utility/Settings.h b/src/utility/Settings.h index 01aa46324..b35e893c4 100644 --- a/src/utility/Settings.h +++ b/src/utility/Settings.h @@ -27,6 +27,8 @@ namespace settings { namespace bpo = boost::program_options; + class Destroyer; + /*! * @brief Wrapper around boost::program_options to handle configuration options. * @@ -43,8 +45,7 @@ namespace settings { * option modules. An option module can be anything that implements the * interface specified by registerModule(). */ - class Settings - { + class Settings { public: /*! @@ -123,13 +124,26 @@ namespace settings { friend std::ostream& helpConfigfile(std::ostream& os); friend Settings* instance(); friend Settings* newInstance(int const argc, char const * const argv[], char const * const filename, bool const sloppy = false); + friend Destroyer; private: /*! - * @brief Constructor. + * @brief Private constructor. + * + * This constructor is private, as noone should be able to create + * an instance manually, one should always use the + * newInstance() method. */ Settings(int const argc, char const * const argv[], char const * const filename, bool const sloppy); + /*! + * @brief Private destructor. + * + * This destructor should be private, as noone should be able to destroy a singleton. + * The object is automatically destroyed when the program terminates by the destroyer. + */ + ~Settings() {} + /*! * @brief Initialize options_description object. */ @@ -174,8 +188,35 @@ namespace settings { * @brief actual instance of this class. */ static Settings* inst; + + /*! + * @brief Destroyer object. + */ + static Destroyer destroyer; }; + /*! + * @brief Destroyer class for singleton object of Settings. + * + * The sole purpose of this class is to clean up the singleton object + * instance of Settings. The Settings class has a static member of this + * Destroyer type that gets cleaned up when the program terminates. In + * it's destructor, this object will remove the Settings instance. + */ + class Destroyer { + public: + /*! + * @brief Destructor. + * + * Free Settings::inst. + */ + ~Destroyer() { + if (Settings::inst != nullptr) { + delete Settings::inst; + } + } + }; + /*! * @brief Print usage help. */ diff --git a/test/storm-tests.cpp b/test/storm-tests.cpp index f2556a286..9165b68a8 100644 --- a/test/storm-tests.cpp +++ b/test/storm-tests.cpp @@ -42,13 +42,11 @@ bool parseOptions(int const argc, char const * const argv[]) { } catch (storm::exceptions::InvalidSettingsException& e) { std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl; std::cout << std::endl << storm::settings::help; - delete s; return false; } if (s->isSet("help")) { std::cout << storm::settings::help; - delete s; return false; }