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.

It is hard to tell without seeing the definitions of g_spEngineLog and BOOSTSP although I am guessing from the name that it is a smart pointer defined by the Boost library.

Assuming that g_spEngineLog is also a BOOSTSP and that BOOSTSP has the normal operation for a smart pointer (i.e. it reference counts and deallocates when there are no references) I don't really see what is happening either.

I would say it is time to break out the symbolic debugger and put a break point in the destructor so you can tell when your object is being destroyed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.