title: C++/Windows: How to report an out-of-memory exception (bad_alloc)?
body: <p>I'm currently working on an exception-based error reporting system for Windows MSVC++ (9.0) apps (i.e. exception structures &amp; types / inheritance, call stack, error reporting &amp; logging and so on).<br>
  <br />
  My question now is: how to correctly report &amp; log an out-of-memory error?<br>
  <br />
  When this error occurs, e.g. as an <code>bad_alloc</code> thrown by the <code>new</code> op, there may be many "features" unavailable, mostly concerning further memory allocation. Normally, I'd pass the exception to the application if it has been thrown in a lib, and then using message boxes and error log files to report and log it. Another way (mostly for services) is to use the Windows Event Log.<br>
  <strong>The main problem I have is to assemble an error message.</strong>
  To provide some error information, I'd like to define a static error message (may be a string literal, better an entry in a message file, then using FormatMessage) and include some run-time info such as a call stack.<br>
  The functions / methods necessary for this use either</p>
  
  <ul>
  <li>STL (<code>std::string, std::stringstream, std::ofstream</code>)</li>
  <li>CRT (<code>swprintf_s, fwrite</code>)</li>
  <li>or Win32 API (<code>StackWalk64, MessageBox, FormatMessage, ReportEvent, WriteFile</code>)</li>
  </ul>
  
  <p>Besides being documented on the MSDN, all of them more (Win32) or less (STL) closed source in Windows, so I don't really know how they behave under low memory problems.<br>
  <br />
  Just to prove there might be problems, I wrote a trivial small app provoking a bad_alloc:</p>
  
  <pre><code>int main()
  {
      InitErrorReporter();  
  
      try
      {
          for(int i = 0; i &lt; 0xFFFFFFFF; i++)
          {
              for(int j = 0; j &lt; 0xFFFFFFFF; j++)
              {
                  char* p = new char;
              }
          }
      }catch(bad_alloc&amp; e_b)
      {
          ReportError(e_b);
      }
  
      DeinitErrorReporter();
  
      return 0;
  }
  </code></pre>
  
  <p>Ran two instances w/o debugger attached (in Release config, VS 2008), but "nothing happened", i.e. no error codes from the ReportEvent or WriteFile I used internally in the error reporting. Then, launched one instance with and one w/o debugger and let them try to report their errors one after the other by using a breakpoint on the ReportError line. That worked fine for the instance with the debugger attached (correctly reported &amp; logged the error, even using LocalAlloc w/o problems)! But taskman showed a strange behaviour, where there's a lot of memory freed before the app exits, I suppose when the exception is thrown.</p>
  
  <p><br />
  Please consider there may be more than one process [edit] and more than one thread [/edit] consuming much memory, so freeing pre-allocated heap space is not a safe solution to avoid a low memory environment for the process which wants to report the error.<br>
  <br /></p>
  
  <p>Thank you in advance!</p>