Hi,
The problem I am having is that the returned object, from a dll factory, is not lasting for more than the function scope that recieves it. Despite reading How to prevent the static initialisation order fiasco I fail to see how it even applies to my code. g_spEngineLog
is the global that is in question. I have declared it as extern in CEngine header and defined it in the implementation. I Hope this is right!
// This is the factory function inside a dll:
extern "C" IMPEXP void* CreateClassInstance(bool bBinary)
{
if (bBinary)
return (static_cast<void*>(new CLog<CFileBinary>));
else
return (static_cast<void*>(new CLog<CFileText>));
} // CreateClassInstance
// This is the function body that uses it. Note that back in main(), or in any other function for that matter, it is not accessable:
{
// Create engine
g_spGlobalEngine = BOOSTSP<CEngine>(new CEngine);
//g_spEngineLog = BOOSTSP<ISerialization>(g_spGlobalEngine->CreateLog(false));
// Create function pointer
fpVoidFunctBool CreateVoidObj;
// Initialise SP with factory function
boost::shared_ptr<CDLLLoader> spDLL(new CDLLLoader(nsStringTable::sPluginLogDLL.c_str()));
// Name of factory function
std::string sFactoryFunction(LPCSTR("CreateClassInstance"));
// Get the function pointer
CreateVoidObj = spDLL->GetFunctionPtr<fpVoidFunctBool>(sFactoryFunction);
// Call factory and initialise GlobalSP with a pointer to the object.
g_spEngineLog = BOOSTSP<ISerialization>(static_cast<ISerialization*>(CreateVoidObj(CENGINELOG_IS_BINARY)));
// Open
std::string sLogFilename(nsStringTable::sEngineLogFilename.c_str());
// For testing purposes
g_spEngineLog->SetNumber(45);
i = g_spEngineLog->GetNumber();
// Log info header
g_spEngineLog->OpenOutput(sLogFilename, std::ios::trunc);
*g_spEngineLog << "CEngine::Create(): '" << sLogFilename <<
"' started on: " << g_spGlobalEngine->GetDate() <<
" at: " << g_spGlobalEngine->GetTime() << std::endl;
}
// Out of scope at the end here.