title: Crash on Exit within std::locale destructor
body: <p>I'm working on a Win32 application in C++ with Visual Studio 2010. When run in debug mode the application runs fine and closes properly upon exit. In release however, the program runs fine but upon application close there's an unhandled exception: Unhandled exception at 0x009C19F5 in Application.exe: 0xC0000005: Access violation reading location 0x00297628.</p>
  
  <p>The debugger then breaks inside xlocale in std::local's destructor:</p>
  
  <pre><code>~locale() _THROW0()
  {  // destroy the object
  if (_Ptr != 0)
      _DELETE_CRT(_Ptr-&gt;_Decref()); // breaks here with unhandled exception
  }
  </code></pre>
  
  <p>The above code is being run, I believe, after my main function returns and exit( 0 ) is called somewhere. Here's my callstack upon crash:</p>
  
  <pre><code>Application.exe!std::locale::~locale() Line 411 C++
  Application.exe!doexit(int code, int quick, int retcaller) Line 567 C
  Application.exe!exit(int code) Line 393 C
  Application.exe!__tmainCRTStartup() Line 284    C
  kernel32.dll!@BaseThreadInitThunk@12()  Unknown
  ntdll.dll!___RtlUserThreadStart@8() Unknown
  ntdll.dll!__RtlUserThreadStart@8()  Unknown
  </code></pre>
  
  <p>I'm using Microsoft's application verifier and I'm running _CrtCheckMemory( ); often to check for heap corruption and I don't see any detected in either debug or release mode. I'm also not messing with std::locale at all in any of my source.</p>
  
  <p>I recently switched the solution's settings to use unicode by default instead of 256 one byte sized characters. However switching back and forth now between unicode and multi-byte settings doesn't seem to affect the crash on exit in release.</p>
  
  <p>Does anyone have any idea what's going on?</p>