Commit 22e941f3 authored by louiz’'s avatar louiz’

Do not “leak” the singleton instance on exit

parent 20258e61
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
#define CONF_DEFAULT_PATH "./batajelo.conf" #define CONF_DEFAULT_PATH "./batajelo.conf"
Config* Config::instance = nullptr; std::unique_ptr<Config> Config::instance;
bool Config::read_conf(const std::string& filename) bool Config::read_conf(const std::string& filename)
{ {
if (instance == 0) if (!instance)
{ {
std::ifstream file; std::ifstream file;
file.open(filename.data()); file.open(filename.data());
...@@ -22,7 +22,7 @@ bool Config::read_conf(const std::string& filename) ...@@ -22,7 +22,7 @@ bool Config::read_conf(const std::string& filename)
return false; return false;
} }
utils::ScopeGuard guard([&file]() {file.close();}); utils::ScopeGuard guard([&file]() {file.close();});
instance = new Config(); instance = std::make_unique<Config>();
instance->filename = filename; instance->filename = filename;
std::string line; std::string line;
...@@ -54,7 +54,7 @@ bool Config::read_conf(const std::string& filename) ...@@ -54,7 +54,7 @@ bool Config::read_conf(const std::string& filename)
std::string Config::get(const std::string& option, const std::string& def) std::string Config::get(const std::string& option, const std::string& def)
{ {
if (instance == nullptr) if (!instance)
{ {
// We can use a default path, only for testing purpose, to avoid // We can use a default path, only for testing purpose, to avoid
// the boring Config::read_conf() at the start of every test suite // the boring Config::read_conf() at the start of every test suite
...@@ -86,7 +86,7 @@ void Config::set_int(const std::string& option, const int& value, bool save) ...@@ -86,7 +86,7 @@ void Config::set_int(const std::string& option, const int& value, bool save)
void Config::set(const std::string& option, const std::string& value, bool save) void Config::set(const std::string& option, const std::string& value, bool save)
{ {
if (instance == nullptr) if (!instance)
{ {
std::cerr << "Error: Config::read_conf() has never been called" << std::endl; std::cerr << "Error: Config::read_conf() has never been called" << std::endl;
return ; return ;
...@@ -114,7 +114,7 @@ void Config::save_to_file() const ...@@ -114,7 +114,7 @@ void Config::save_to_file() const
void Config::connect(t_config_changed_callback callback) void Config::connect(t_config_changed_callback callback)
{ {
if (instance == nullptr) if (!instance)
{ {
std::cerr << "Error: Config::read_conf() has never been called" << std::endl; std::cerr << "Error: Config::read_conf() has never been called" << std::endl;
return ; return ;
...@@ -129,8 +129,7 @@ void Config::close(bool save) ...@@ -129,8 +129,7 @@ void Config::close(bool save)
if (save) if (save)
instance->save_to_file(); instance->save_to_file();
instance->values.clear(); instance->values.clear();
delete instance; instance.reset(nullptr);
instance = nullptr;
} }
void Config::trigger_configuration_change() void Config::trigger_configuration_change()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment