EDIT: Solved see post #13
Hi I have successfully created singleton with a Logging function that works well if I let it leak - but upon deleting the s_pLogInstance, statically newed, object it crashes (no matter where I delete it. - except for right after i create it in the same function): Is it the method by which I access it. Although unlikely, am I recreating it before I destroy it?
Unhandled exception at 0x51E8F069 (msvcp110d.dll) in DXTB.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
Even if I delete the log (pLogInstance) before the singleton (CEngine). I create the CEngine object first then log.
Here is CEngine.h...
#define CENGINE CEngine::GetInstance()
class CEngine
{
public:
~CEngine(void) {}
static void Destroy(void)
{
delete &CEngine::GetInstance();
delete &(CEngine::GetInstance().GetLog()); // <== CRASH HERE pLogInstance is FEEEFEEE
}
static CEngine& GetInstance(void)
{
static CEngine* s_pEngineInstance = new CEngine(); // Runs only once
return *s_pEngineInstance;
}
ISerialization& GetLog(void)
{
static ISerialization* s_pLogInstance = new CFileText(); // Runs only once
return *s_pLogInstance;
}
static void __cdecl DestroyLog(void) // Can't call this from atexit() -dunno how...!
{
delete &CEngine::GetInstance().GetLog();
}
std::string GetDate(void);
std::string GetTime(void);
protected:
CEngine(void) // Hidden constructor
{
GetLog() << "CEngine::CEngine() Created."; // Create log before CEngine to ensure destructor comes after CEngine.
}
private:
// PRIVATE SEMANTICS
CEngine(const CEngine& other) {} // Copy constructor
friend void swap(CEngine& first, CEngine& second) {} // Swap implementation
CEngine& operator=(CEngine other) {} // Assignment operator
CEngine(CEngine&& other) {} // Move constructor
};
...and main() is effectively this (near enough)...
int main(void)
{
CEngine::GetInstance();
CENGINE.GetLog().OpenOutput(sLogFilename, std::ios::trunc);
CENGINE.GetLog() << "CEngine::Create(): '" << sLogFilename <<
"' started on: " << CENGINE.GetDate() <<
" at: " << CENGINE.GetTime() << std::endl;
CENGINE.GetLog() << std::endl;
CENGINE.GetLog().CloseIO();
CENGINE.Destroy();
//atexit(CEngine::DestroyLog()); // <== I cant make this work either. 'static void DestroyLog(void) {}'
//std::cin.get();
return 0;
}
Hmm I know I really should handle exception elegantly but I'm just getting the hang of things. Advice would be appreciated. All help is graciously praised for. :-)