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) {
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<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;
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

47
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,6 +188,33 @@ 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;
}
}
};
/*!

2
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;
}

Loading…
Cancel
Save