diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h index 07ac7d8ce..6fac2c8a9 100644 --- a/indra/llcommon/llsingleton.h +++ b/indra/llcommon/llsingleton.h @@ -176,25 +176,11 @@ public: { SingletonInstanceData& data = getData(); - if (data.mInitState == CONSTRUCTING) + if (data.mInitState != INITIALIZED) { - llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl; + createInstance(data); } - if (data.mInitState == DELETED) - { - llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl; - } - - if (!data.mSingletonInstance) - { - data.mInitState = CONSTRUCTING; - data.mSingletonInstance = new DERIVED_TYPE(); - data.mInitState = INITIALIZING; - data.mSingletonInstance->initSingleton(); - data.mInitState = INITIALIZED; - } - return data.mSingletonInstance; } @@ -220,7 +206,35 @@ public: } private: + static void createInstance(SingletonInstanceData& data); virtual void initSingleton() {} }; +// Moved this here cause it's too big to be inlined --Aleric. +template +void LLSingleton::createInstance(SingletonInstanceData& data) +{ + if (data.mInitState == CONSTRUCTING) + { + llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl; + } + + if (data.mInitState == DELETED) + { + llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl; + } + + if (data.mInitState == INITIALIZING) + { + llwarns << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from initSingleton(), using half-initialized object" << llendl; + return; + } + + data.mInitState = CONSTRUCTING; + data.mSingletonInstance = new DERIVED_TYPE(); + data.mInitState = INITIALIZING; + data.mSingletonInstance->initSingleton(); + data.mInitState = INITIALIZED; +} + #endif