My code below is meant to be a simple error logging system that behaves in a way that is similar to printf.
All of my code has been running fine in a gtest environment, but now as I exit a deterministic point in the program (one of my tests) it stack smashes. My code has been running fine up until I wrote this and it is my first foray into cstdarg so it is the most suspect.
MyMutex error_lock;
#define MAX_ERR_MSG_SIZE 128
#define MAX_ERRORS 3
class ErrorQueue
{
std::queue<char*> errors;
std::list<char*> old_errors;
public:
void push(char * msg)
{
if (errors.size() >= MAX_ERRORS)
{
pop();
}
errors.push(msg);
}
void pop()
{
if (old_errors.size() >= MAX_ERRORS)
{
delete [] old_errors.front();
old_errors.pop_front();
}
old_errors.push_back(errors.front());
errors.pop();
}
char * front()
{
return errors.front();
}
size_t size()
{
return errors.size();
}
~ErrorQueue()
{
while(!errors.empty())
{
delete [] errors.front();
errors.pop();
}
while (!old_errors.empty())
{
delete [] old_errors.front();
old_errors.pop_front();
}
}
};
static ErrorQueue errors;
void WriteCallError(const char * error_message, ...)
{
char err_buffer[MAX_ERR_MSG_SIZE];
va_list args;
va_start(args,error_message);
std::vsnprintf(err_buffer,MAX_ERR_MSG_SIZE,error_message,args);
va_end(args);
char * err_string = new char[MAX_ERR_MSG_SIZE];
memcpy(err_string,err_buffer,MAX_ERR_MSG_SIZE);
{
error_lock.Lock();
errors.push(err_string);
error_lock.Unlock();
}
}
I call WriteCallError numerous times elsewhere in the code and after a certain number of times it pukes and tells me I've stack smashed.
Where is my fault? Is there some funky interaction between cstdarg and gtest? Is there even enough information here?
Edit: Using a simple main I tried isolating it:
int main (int argc, char ** argv)
{
int i = 0;
while (1)
{
WriteCallError("Breaks on %d",i++);
}
}
This will not cause a stack smash.