From 0dc6a082ca666937da0344fba0f9471cb326630a Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sun, 23 May 2010 06:58:11 +0000 Subject: [PATCH] Fix memory leaks in Option Originally committed to SVN as r4349. --- aegisub/libaegisub/common/option.cpp | 11 +++++++---- aegisub/libaegisub/common/option_visit.cpp | 23 ++++++++++++++-------- aegisub/libaegisub/lagi_pre.h | 1 + 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/aegisub/libaegisub/common/option.cpp b/aegisub/libaegisub/common/option.cpp index e61e1e6cb..c74ef18c5 100644 --- a/aegisub/libaegisub/common/option.cpp +++ b/aegisub/libaegisub/common/option.cpp @@ -37,6 +37,9 @@ Options::Options(const std::string &file, const std::string& default_config): Options::~Options() { Flush(); + for (OptionValueMap::iterator i = values.begin(); i != values.end(); i++) { + delete i->second; + } } void Options::ConfigNext(std::istream& stream) { @@ -60,9 +63,9 @@ void Options::LoadConfig(std::istream& stream) { json::Reader::Read(config_root, stream); } catch (json::Reader::ParseException& e) { std::cout << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; - } catch (json::Exception& e) { - /// @todo Do something better here, maybe print the exact error - std::cout << "json::Exception: " << e.what() << std::endl; + } catch (json::Exception& e) { + /// @todo Do something better here, maybe print the exact error + std::cout << "json::Exception: " << e.what() << std::endl; } ConfigVisitor config_visitor(values, std::string("")); @@ -76,7 +79,7 @@ OptionValue* Options::Get(const std::string &name) { OptionValueMap::iterator index; - if ((index = values.find(name)) != values.end()) + if ((index = values.find(name)) != values.end()) return index->second; std::cout << "agi::Options::Get Option not found: (" << name << ")" << std::endl; diff --git a/aegisub/libaegisub/common/option_visit.cpp b/aegisub/libaegisub/common/option_visit.cpp index 1609a9b7d..c5e513d47 100644 --- a/aegisub/libaegisub/common/option_visit.cpp +++ b/aegisub/libaegisub/common/option_visit.cpp @@ -22,6 +22,8 @@ #ifndef LAGI_PRE #include +#include + #include #include #endif @@ -32,7 +34,7 @@ namespace agi { ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name): values(val) { - // Corropsonding code is in AddOptionValue() + // Corresponding code is in AddOptionValue() name = member_name + "/"; } @@ -57,9 +59,9 @@ void ConfigVisitor::Visit(const json::Object& object) { void ConfigVisitor::Visit(const json::Array& array) { - int init = 0; + bool init = false; - OptionValueList *array_list; + OptionValueList *array_list = NULL; json::Array::const_iterator index(array.Begin()), indexEnd(array.End()); @@ -90,7 +92,7 @@ void ConfigVisitor::Visit(const json::Array& array) { } else { throw OptionJsonValueArray("Array type not handled"); } - init = 1; + init = true; } try { @@ -115,16 +117,17 @@ void ConfigVisitor::Visit(const json::Array& array) { Colour col(val); array_list->InsertColour(col); } - - AddOptionValue(array_list); - } catch (agi::Exception&) { + delete array_list; throw OptionJsonValueArray("Attempt to insert value into array of wrong type"); } } // for index_object } // for index + + if (array_list) AddOptionValue(array_list); + } @@ -165,7 +168,7 @@ void ConfigVisitor::Visit(const json::Null& null) { void ConfigVisitor::AddOptionValue(OptionValue* opt) { - // Corrosponding code is in the constuctor. + // Corresponding code is in the constuctor. std::string stripped = name.substr(1, name.rfind("/")-1); OptionValue *opt_cur; @@ -178,6 +181,10 @@ void ConfigVisitor::AddOptionValue(OptionValue* opt) { return; } + // Ensure than opt is deleted at the end of this function even if the Set + // method throws + std::auto_ptr auto_opt(opt); + int type = opt_cur->GetType(); switch (type) { case OptionValue::Type_String: diff --git a/aegisub/libaegisub/lagi_pre.h b/aegisub/libaegisub/lagi_pre.h index 638e7168e..9e4155a8b 100644 --- a/aegisub/libaegisub/lagi_pre.h +++ b/aegisub/libaegisub/lagi_pre.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include