Browse Source

Fixed memory leaks involving Settings class

Settings (being a singleton) will now free it's instance itself upon program termination.
tempestpy_adaptions
gereon 12 years ago
parent
commit
f9923bac95
  1. 7
      src/storm.cpp
  2. 2
      src/utility/Settings.cpp
  3. 47
      src/utility/Settings.h
  4. 2
      test/storm-tests.cpp

7
src/storm.cpp

@ -99,18 +99,15 @@ bool parseOptions(const int argc, const char* argv[]) {
} catch (storm::exceptions::InvalidSettingsException& e) { } catch (storm::exceptions::InvalidSettingsException& e) {
std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl; std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl;
std::cout << std::endl << storm::settings::help; std::cout << std::endl << storm::settings::help;
delete s;
return false; return false;
} }
if (s->isSet("help")) { if (s->isSet("help")) {
std::cout << storm::settings::help; std::cout << storm::settings::help;
delete s;
return false; return false;
} }
if (s->isSet("test-prctl")) { if (s->isSet("test-prctl")) {
storm::parser::PrctlParser parser(s->getString("test-prctl").c_str()); storm::parser::PrctlParser parser(s->getString("test-prctl").c_str());
delete s;
return false; return false;
} }
@ -133,9 +130,7 @@ bool parseOptions(const int argc, const char* argv[]) {
* Function to perform some cleanup. * Function to perform some cleanup.
*/ */
void cleanUp() { void cleanUp() {
if (storm::settings::instance() != nullptr) {
delete storm::settings::instance();
}
// nothing here
} }
void testCheckingDie(storm::models::Dtmc<double>& dtmc) { void testCheckingDie(storm::models::Dtmc<double>& dtmc) {

2
src/utility/Settings.cpp

@ -33,6 +33,8 @@ storm::settings::Settings* storm::settings::Settings::inst = nullptr;
std::map< std::pair<std::string, std::string>, std::shared_ptr<bpo::options_description> > storm::settings::Settings::modules; std::map< std::pair<std::string, std::string>, std::shared_ptr<bpo::options_description> > storm::settings::Settings::modules;
storm::settings::Destroyer storm::settings::Settings::destroyer;
/*! /*!
* The constructor fills the option descriptions, parses the * The constructor fills the option descriptions, parses the
* command line and the config file and puts the option values to * command line and the config file and puts the option values to

47
src/utility/Settings.h

@ -27,6 +27,8 @@ namespace settings {
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
class Destroyer;
/*! /*!
* @brief Wrapper around boost::program_options to handle configuration options. * @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 * option modules. An option module can be anything that implements the
* interface specified by registerModule(). * interface specified by registerModule().
*/ */
class Settings
{
class Settings {
public: public:
/*! /*!
@ -123,13 +124,26 @@ namespace settings {
friend std::ostream& helpConfigfile(std::ostream& os); friend std::ostream& helpConfigfile(std::ostream& os);
friend Settings* instance(); friend Settings* instance();
friend Settings* newInstance(int const argc, char const * const argv[], char const * const filename, bool const sloppy = false); friend Settings* newInstance(int const argc, char const * const argv[], char const * const filename, bool const sloppy = false);
friend Destroyer;
private: 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); 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. * @brief Initialize options_description object.
*/ */
@ -174,8 +188,35 @@ namespace settings {
* @brief actual instance of this class. * @brief actual instance of this class.
*/ */
static Settings* inst; 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. * @brief Print usage help.
*/ */

2
test/storm-tests.cpp

@ -42,13 +42,11 @@ bool parseOptions(int const argc, char const * const argv[]) {
} catch (storm::exceptions::InvalidSettingsException& e) { } catch (storm::exceptions::InvalidSettingsException& e) {
std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl; std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl;
std::cout << std::endl << storm::settings::help; std::cout << std::endl << storm::settings::help;
delete s;
return false; return false;
} }
if (s->isSet("help")) { if (s->isSet("help")) {
std::cout << storm::settings::help; std::cout << storm::settings::help;
delete s;
return false; return false;
} }

Loading…
Cancel
Save