title: ofstream::write() unhandled exception
body: <p>So I'm writing a logging library for logging all sorts of things, and when I ran a test on it, it kept on crashing. I narrowed the exception down to the writing function when I write the log message to the ofstream file. I parse the message and stuff, and then I have the actual call to ofstream::write(). here is the part where I get a reuntime error:</p>
  
  <pre><code>void Logger::writeMessage(LogMessage* message)
  {
      if(message==NULL)
          return;
      char buffer[MAX_PATH];
      switch(message-&gt;GetMessageType())
      {
      case LOGMESSAGE_HEADER:
          sprintf(buffer, m_logInfo-&gt;headerFormat, message-&gt;GetMessage().c_str());
          break;
      case LOGMESSAGE_FOOTER:
          sprintf(buffer, m_logInfo-&gt;footerFormat, message-&gt;GetMessage().c_str());
          break;
      case LOGMESSAGE_DEBUG:
          sprintf(buffer, "%s %s", m_logInfo-&gt;debugPrefix.c_str(), message-&gt;GetMessage().c_str());
          break;
      case LOGMESSAGE_ADDRESS:
          sprintf(buffer, "%s %s", m_logInfo-&gt;addressPrefix.c_str(), message-&gt;GetMessage().c_str());
          break;
      case LOGMESSAGE_VALUE:
          sprintf(buffer, "%s %s", m_logInfo-&gt;valuePrefix.c_str(), message-&gt;GetMessage().c_str());
          break;
      case LOGMESSAGE_CUSTOM:
      default:
          sprintf(buffer, "test!", message-&gt;GetMessage().c_str());
          break;
      }
      try
      {
          if(!m_ofile.is_open() || !m_ofile.good())
              return;
  
          //string formattedMessage(buffer);
          //formattedMessage.append(m_logInfo-&gt;lineTerminator);
  
          string result;
          if(message-&gt;IsUsingTimestamp())
          {
              m_ofile &lt;&lt; message-&gt;GetTimeStamp().GetTimeString().c_str() &lt;&lt; " ";
              //result.append(message-&gt;GetTimeStamp().GetTimeString());
              //result.append(" ");
          }
  
          m_ofile &lt;&lt; buffer &lt;&lt; m_logInfo-&gt;lineTerminator;
  
          //result.append(formattedMessage);
          //result.push_back('\0');
  
          //m_ofile.write(result.c_str(), MAX_PATH);
          //m_ofile &lt;&lt; result.c_str();
      } 
      catch(std::exception &amp;e)
      {
          MessageBox(NULL, e.what(), "ERROR", NULL);
      }
  }
  </code></pre>
  
  <p>as you can see, I have the call in a try catch block and I even check if the file is valid and open. When I set breakpoints on the call and all around it, the call works fine, but once it reaches the end of the function it gives me this:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x773515ee in LoggerTest.exe: 0xC0000005: Access violation writing location 0xfeeefeee.</p>
  </blockquote>
  
  <p>and then it shows the error to occur in this function inside xlock.cpp:</p>
  
  <pre><code>__thiscall _Lockit::_Lockit(int kind)
      : _Locktype(kind)
      {   // lock the mutex
      if (_Locktype &lt; MAX_LOCK)
          _Mtxlock(&amp;mtx[_Locktype]);
      }
  </code></pre>
  
  <p>My guess is that I have a bad string or pointer somewhere, but I can't pinpoint it.</p>
  
  <p>NOTE: I tried doing </p>
  
  <pre><code>m_ofile &lt;&lt; "test!";
  </code></pre>
  
  <p>and now it gives me assert failure here: <code>_ASSERTE(_CrtIsValidHeapPointer(pUserData));</code></p>