From 4ea546a91e7188ad6a95931637b7b928abd38342 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 18 Feb 2013 22:01:15 +0100 Subject: Fix unspecified construction order of Lua::s_init_functions --- src/FbTk/LuaUtil.cc | 23 ++++++++++++++++++++--- src/FbTk/LuaUtil.hh | 15 ++++----------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/FbTk/LuaUtil.cc b/src/FbTk/LuaUtil.cc index 700ef1a..8f382e8 100644 --- a/src/FbTk/LuaUtil.cc +++ b/src/FbTk/LuaUtil.cc @@ -57,9 +57,26 @@ namespace { return 0; } + struct AutoVector: public std::vector { + ~AutoVector() { + for(iterator it = begin(); it != end(); ++it) + delete *it; + } + }; + typedef AutoVector InitFunctions; + + InitFunctions *s_init_functions; } // anonymous namespace -Lua::InitFunctions Lua::s_init_functions; +void Lua::doRegisterInitFunction(std::auto_ptr fn) { + struct InitHelper { + InitHelper() { s_init_functions = new InitFunctions; } + ~InitHelper() { delete s_init_functions; } + }; + static InitHelper helper; + + s_init_functions->push_back(fn.release()); +} Lua::Lua() { checkstack(1); @@ -73,8 +90,8 @@ Lua::Lua() { makeReadOnly(lua::GLOBALSINDEX, true); - InitFunctions::const_iterator it_end = s_init_functions.end(); - for(InitFunctions::const_iterator it = s_init_functions.begin(); it != it_end; ++it) + InitFunctions::const_iterator it_end = s_init_functions->end(); + for(InitFunctions::const_iterator it = s_init_functions->begin(); it != it_end; ++it) (**it)(*this); } diff --git a/src/FbTk/LuaUtil.hh b/src/FbTk/LuaUtil.hh index d013ed9..12a687e 100644 --- a/src/FbTk/LuaUtil.hh +++ b/src/FbTk/LuaUtil.hh @@ -53,9 +53,11 @@ public: */ void readOnlySet(int index); + typedef Slot InitFunction; + template static void registerInitFunction(const Functor &fn) { - s_init_functions.push_back(new SlotImpl(fn)); + doRegisterInitFunction(std::auto_ptr(new SlotImpl(fn))); } class RegisterInitFunction { @@ -67,16 +69,7 @@ public: }; private: - typedef Slot InitFunction; - struct AutoVector: public std::vector { - ~AutoVector() { - for(iterator it = begin(); it != end(); ++it) - delete *it; - } - }; - typedef AutoVector InitFunctions; - - static InitFunctions s_init_functions; + static void doRegisterInitFunction(std::auto_ptr fn); }; } // namespace FbTk -- cgit v0.11.2