From 81d1073a97c15ef5f1ba6317353ff9895ba88b79 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 27 Apr 2012 19:08:17 +0000 Subject: [PATCH] Move the auto4 script factories to a local non-heap static to reduce memory leak report noise Originally committed to SVN as r6728. --- aegisub/src/auto4_base.cpp | 44 ++++++++++++++++---------------------- aegisub/src/auto4_base.h | 5 ++--- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/aegisub/src/auto4_base.cpp b/aegisub/src/auto4_base.cpp index 3eb008f30..91633ee73 100644 --- a/aegisub/src/auto4_base.cpp +++ b/aegisub/src/auto4_base.cpp @@ -530,8 +530,6 @@ namespace Automation4 { } // ScriptFactory - std::vector *ScriptFactory::factories = 0; - ScriptFactory::ScriptFactory(wxString engine_name, wxString filename_pattern) : engine_name(engine_name) , filename_pattern(filename_pattern) @@ -540,30 +538,24 @@ namespace Automation4 { void ScriptFactory::Register(ScriptFactory *factory) { - GetFactories(); - - if (find(factories->begin(), factories->end(), factory) != factories->end()) + if (find(Factories().begin(), Factories().end(), factory) != Factories().end()) throw agi::InternalError("Automation 4: Attempt to register the same script factory multiple times. This should never happen.", 0); - factories->push_back(factory); + Factories().push_back(factory); } void ScriptFactory::Unregister(ScriptFactory *factory) { - if (!factories) return; - - std::vector::iterator i = find(factories->begin(), factories->end(), factory); - if (i != factories->end()) { + std::vector::iterator i = find(Factories().begin(), Factories().end(), factory); + if (i != Factories().end()) { delete *i; - factories->erase(i); + Factories().erase(i); } } Script* ScriptFactory::CreateFromFile(wxString const& filename, bool log_errors) { - GetFactories(); - - for (std::vector::iterator i = factories->begin(); i != factories->end(); ++i) { + for (std::vector::iterator i = Factories().begin(); i != Factories().end(); ++i) { Script *s = (*i)->Produce(filename); if (s) { if (!s->GetLoadedState() && log_errors) @@ -583,26 +575,26 @@ namespace Automation4 { { using std::tr1::placeholders::_1; // Just make this always return true to bitch about unknown script formats in autoload - GetFactories(); - return find_if(factories->begin(), factories->end(), - bind(&wxString::Matches, filename, bind(&ScriptFactory::GetFilenamePattern, _1))) != factories->end(); + return find_if(Factories().begin(), Factories().end(), + bind(&wxString::Matches, filename, bind(&ScriptFactory::GetFilenamePattern, _1))) != Factories().end(); + } + + std::vector& ScriptFactory::Factories() + { + static std::vector factories; + return factories; } const std::vector& ScriptFactory::GetFactories() { - if (!factories) - factories = new std::vector(); - - return *factories; + return Factories(); } wxString ScriptFactory::GetWildcardStr() { - GetFactories(); - wxString fnfilter, catchall; - for (size_t i = 0; i < factories->size(); ++i) { - const ScriptFactory *fact = (*factories)[i]; + for (size_t i = 0; i < Factories().size(); ++i) { + const ScriptFactory *fact = Factories()[i]; if (fact->GetEngineName().empty() || fact->GetFilenamePattern().empty()) continue; @@ -614,7 +606,7 @@ namespace Automation4 { if (!catchall.empty()) catchall.RemoveLast(); - if (factories->size() > 1) + if (Factories().size() > 1) fnfilter = _("All Supported Formats") + "|" + catchall + "|" + fnfilter; return fnfilter; diff --git a/aegisub/src/auto4_base.h b/aegisub/src/auto4_base.h index 6fa3a3d65..4cd0651d3 100644 --- a/aegisub/src/auto4_base.h +++ b/aegisub/src/auto4_base.h @@ -256,9 +256,6 @@ namespace Automation4 { /// Both a base class for script factories and a manager of registered /// script factories class ScriptFactory { - /// Vector of loaded script engines - static std::vector *factories; - wxString engine_name; wxString filename_pattern; @@ -271,6 +268,8 @@ namespace Automation4 { /// CreateFromFile virtual Script* Produce(wxString const& filename) const = 0; + static inline std::vector& Factories(); + protected: ScriptFactory(wxString engine_name, wxString filename_pattern); virtual ~ScriptFactory() { }