I'm trying to build a singleton class which will hold a cyclic buffer , every time some function wants to dump send something to a debug client (e.g DBGview or similar) the function will store its information inside the cyclic buffer. the object will also run a thread that will sleep for some time and then wake up and dump the buffer contents to the debug client.
this is the function everyone calls when need to send something to debug:
TTFW_LogRet PrintToLogger (std::string Str)
{
Logger *Log;
if(Instance_Mutex == NULL)
{
Instance_Mutex = CreateMutex(NULL,FALSE,NULL);
if (Instance_Mutex == NULL)
{
OutputDebugStringA("CreateMutex error! the Logger will close \n");
return GENERAL_ERROR;
}
}
Log = Logger::getLogInstance();
Log->LogStringToOutput(Str);
return SUCCESS;
}
for some reason i always get a heap corruption errors after the return SUCCESS statement
this is the Getinstance() function:
Logger* Logger::getLogInstance()
{
if(_instance == NULL)
{
MutexLock(Instance_Mutex);
if(_instance == NULL)
{
_instance = new Logger();
}
MutexUnlock(Instance_Mutex);
}
return _instance;
}
and the thread function is:
DWORD WINAPI Logger::WorkerThread (LPVOID lpParam )
{
Logger *log = static_cast <Logger*> (lpParam);
for(;;)
{
Sleep(2000);
if(log->getOutputMethod() == odBuffer && log->CyclicBuffer1.size() >= log->Thresh && !log->CyclicBuffer1.empty())
{
TTFW_LogRet ret;
ret = log->FullBufferDump();
if (ret != SUCCESS)
{
log->LogStringToOutput("Error occured in dumping cyclic buffer , the buffer will be cleared\n");
}
}
}
}
does anybody have any idea why would a heap corruption occur with that code? note that the function to debug gets called many times and it is no synchronized with the thread who dumps the buffer.