From 2c01015a9da9d7f4a27aa0c7092dcdc7768d9d36 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 11 Feb 2011 21:29:46 +0000 Subject: Use a static local var for the loader map to work around the link/load order issue --- project2/xmlObjectLoader.cpp | 33 ++++++++++++--------------------- project2/xmlObjectLoader.h | 8 ++------ 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp index a40c66c..f14f773 100644 --- a/project2/xmlObjectLoader.cpp +++ b/project2/xmlObjectLoader.cpp @@ -6,17 +6,12 @@ unsigned int LoaderBase::depth = 0; std::set LoaderBase::loadedObjects; -typedef std::map ElementLoaderMap; -static ElementLoaderMap * objLoaders; -void init_loaders() __attribute__ ((constructor(101))); -void kill_loaders() __attribute__ ((destructor(101))); -void init_loaders() +typedef std::map > ElementLoaderMap; +ElementLoaderMap & +objLoaders() { - objLoaders = new ElementLoaderMap(); -} -void kill_loaders() -{ - delete objLoaders; + static ElementLoaderMap _objLoaders; + return _objLoaders; } LoaderBase::LoaderBase(const Glib::ustring & n, bool r) : @@ -40,8 +35,8 @@ LoaderBase::collectAll(const xmlpp::Element * node, bool childrenOnly, Unsupport if (!childrenOnly && node->get_namespace_uri() == ns) { Glib::ustring name = node->get_name(); unsigned int stored = 0; - ElementLoaderMap::const_iterator i = objLoaders->find(name); - if (i != objLoaders->end()) { + ElementLoaderMap::const_iterator i = objLoaders().find(name); + if (i != objLoaders().end()) { SourceObjectPtr o = i->second->go(node); created += 1; loadedObjects.insert(o); @@ -87,23 +82,19 @@ LoaderBase::collectAll(const CommonObjects * co, const xmlpp::Element * node, bo void LoaderBase::newLoader(const std::string & n, ElementLoader * l) { - objLoaders->insert(ElementLoaderMap::value_type(n, l)); + objLoaders().insert(ElementLoaderMap::value_type(n, boost::shared_ptr(l))); } void LoaderBase::removeLoader(const std::string & n) { - ElementLoaderMap::const_iterator i = objLoaders->find(n); - if (i != objLoaders->end()) { - delete i->second; - } - objLoaders->erase(n); + objLoaders().erase(n); } void LoaderBase::onIdle() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onIdle(); } } @@ -115,7 +106,7 @@ LoaderBase::onIteration() depth = 0; loadedObjects.clear(); - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onIteration(); } } @@ -123,7 +114,7 @@ LoaderBase::onIteration() void LoaderBase::onPeriodic() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onPeriodic(); } } diff --git a/project2/xmlObjectLoader.h b/project2/xmlObjectLoader.h index 2ec44e1..7dec077 100644 --- a/project2/xmlObjectLoader.h +++ b/project2/xmlObjectLoader.h @@ -115,14 +115,10 @@ class LoaderBase { #define DECLARE_CUSTOM_LOADER(N, T) \ static void init_loader_##T() __attribute__ ((constructor(201))); \ - static void kill_loader_##T() __attribute__ ((destructor(201))); \ - static void init_loader_##T() { LoaderBase::newLoader(N, new T()); } \ - static void kill_loader_##T() { LoaderBase::removeLoader(N); } + static void init_loader_##T() { LoaderBase::newLoader(N, new T()); } #define DECLARE_LOADER(N, T) \ static void init_loader_##T() __attribute__ ((constructor(201))); \ - static void kill_loader_##T() __attribute__ ((destructor(201))); \ - static void init_loader_##T() { LoaderBase::newLoader(N, new ElementLoaderImpl()); } \ - static void kill_loader_##T() { LoaderBase::removeLoader(N); } + static void init_loader_##T() { LoaderBase::newLoader(N, new ElementLoaderImpl()); } class ElementLoader { public: -- cgit v1.2.3