From 82969687f87a44f657c9a36ae0433a91099ca598 Mon Sep 17 00:00:00 2001 From: Gasper Jansa Date: Mon, 31 Jan 2011 21:17:42 +0100 Subject: [PATCH] fixed memory leak. System configuration gets created by default. --- pvAccessApp/utils/configuration.cpp | 20 +++++++++++++------- pvAccessApp/utils/configuration.h | 15 +++++++++++++-- testApp/utils/configurationTest.cpp | 1 - 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pvAccessApp/utils/configuration.cpp b/pvAccessApp/utils/configuration.cpp index f68aaf6..f0ee2a8 100644 --- a/pvAccessApp/utils/configuration.cpp +++ b/pvAccessApp/utils/configuration.cpp @@ -58,7 +58,7 @@ string Properties::getProperty(const string key) else { string errMsg = "Property not found in the map: " + key; - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } } @@ -84,7 +84,7 @@ void Properties::load() } catch (ifstream::failure& e) { string errMsg = "Error opening file: " + string(_fileName.c_str()); - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } string line; @@ -116,7 +116,7 @@ void Properties::load() if(pos == string::npos) //bad value (= not found) { string errMsg = "Bad property line found: " + line; - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } key = line.substr(0,pos); @@ -134,7 +134,7 @@ void Properties::load() return; //end of file } string errMsg = "Error reading file: " + _fileName; - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } _infile->close(); } @@ -153,7 +153,7 @@ void Properties::store() } catch (ofstream::failure& e) { string errMsg = "Error opening file: " + string(_fileName.c_str()); - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } @@ -169,7 +169,7 @@ void Properties::store() catch (ofstream::failure& e) { _outfile->close(); string errMsg = "Error writing to file: " + string(_fileName.c_str()); - throw BaseException(errMsg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(errMsg.c_str()); } } _outfile->close(); @@ -272,6 +272,10 @@ ConfigurationProviderImpl::ConfigurationProviderImpl() ConfigurationProviderImpl::~ConfigurationProviderImpl() { + for(_configsIter = _configs.begin(); _configsIter != _configs.end(); _configsIter++) + { + delete _configsIter->second; + } _configs.clear(); } @@ -282,7 +286,7 @@ void ConfigurationProviderImpl::registerConfiguration(const string name, const C if(_configsIter != _configs.end()) { string msg = "configuration with name " + name + " already registered"; - throw BaseException(msg.c_str(), __FILE__, __LINE__); + THROW_BASE_EXCEPTION(msg.c_str()); } _configs[name] = configuration; } @@ -306,6 +310,8 @@ ConfigurationProviderImpl* ConfigurationFactory::getProvider() if(_configurationProvider == NULL) { _configurationProvider = new ConfigurationProviderImpl(); + // default + _configurationProvider->registerConfiguration("system", new SystemConfigurationImpl()); } return _configurationProvider; } diff --git a/pvAccessApp/utils/configuration.h b/pvAccessApp/utils/configuration.h index 02754e4..74a4363 100644 --- a/pvAccessApp/utils/configuration.h +++ b/pvAccessApp/utils/configuration.h @@ -72,6 +72,10 @@ private: class Configuration : private epics::pvData::NoDefaultMethods { public: + /* + * Destructor. + */ + virtual ~Configuration() {}; /* * Get the environment variable specified by name or return default value * if it does not exist. @@ -128,7 +132,7 @@ class SystemConfigurationImpl: public Configuration { public: SystemConfigurationImpl(); - virtual ~SystemConfigurationImpl(); + ~SystemConfigurationImpl(); bool getPropertyAsBoolean(const std::string name, const bool defaultValue); epics::pvData::int32 getPropertyAsInteger(const std::string name, const epics::pvData::int32 defaultValue); float getPropertyAsFloat(const std::string name, const float defaultValue); @@ -148,6 +152,10 @@ private: class ConfigurationProvider : private epics::pvData::NoDefaultMethods { public: + /* + * Destructor. + */ + virtual ~ConfigurationProvider() {}; /* * Return configuration specified by name. * @@ -169,7 +177,10 @@ class ConfigurationProviderImpl: public ConfigurationProvider { public: ConfigurationProviderImpl(); - virtual ~ConfigurationProviderImpl(); + /* + * Destructor. Note: Registered configurations will be deleted!! + */ + ~ConfigurationProviderImpl(); Configuration* getConfiguration(const std::string name); void registerConfiguration(const std::string name, const Configuration* configuration); private: diff --git a/testApp/utils/configurationTest.cpp b/testApp/utils/configurationTest.cpp index ff69124..6afec88 100644 --- a/testApp/utils/configurationTest.cpp +++ b/testApp/utils/configurationTest.cpp @@ -67,7 +67,6 @@ int main(int argc, char *argv[]) doubleProperty = configuration->getPropertyAsDouble("dobuleProperty1", -3); assert(doubleProperty == 42); - if(configuration) delete configuration; if(configProvider) delete configProvider; getShowConstructDestruct()->constuctDestructTotals(stdout); return 0;