title: Is there a bad interaction between gtest and cstdarg, or am I really missing the stack smash here?
body: <p>My code below is meant to be a simple error logging system that behaves in a way that is similar to printf.</p>
  
  <p>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.</p>
  
  <pre><code>MyMutex error_lock;
  
  #define MAX_ERR_MSG_SIZE 128
  #define MAX_ERRORS 3
  class ErrorQueue
  {
     std::queue&lt;char*&gt; errors;
     std::list&lt;char*&gt; old_errors;
  public:
     void push(char * msg)
     {
        if (errors.size() &gt;= MAX_ERRORS)
        {
           pop();
        }
        errors.push(msg);
     }
  
     void pop()
     {
        if (old_errors.size() &gt;= 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();
     }
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>Where is my fault? Is there some funky interaction between cstdarg and gtest? Is there even enough information here?</p>
  
  <p>Edit: 
  Using a simple main I tried isolating it:</p>
  
  <pre><code>int main (int argc, char ** argv)
  {
     int i = 0;
     while (1)
     {
        WriteCallError("Breaks on %d",i++);
     }
  
  }
  </code></pre>
  
  <p>This will not cause a stack smash.</p>