title: AV on ctrl-c in destructor of global variable
body: <p>Consider following snippet:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  
  using namespace std;
  
  class A
  {
  public:
      ~A()
      {
          cout &lt;&lt; "I am about to die" &lt;&lt; endl;
      }
  };
  
  A a;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      while (true)
      {
          cout &lt;&lt; "Waiting for ctrlc" &lt;&lt; endl;
          Sleep(1000);
      }
  }
  </code></pre>
  
  <p>When I press ctrl-c I get an AV with following callstack:</p>
  
  <pre><code>0:001&gt; kn
   # Child-SP          RetAddr           Call Site
  00 000000c0`dcdbf420 000007fe`9244fab9 MSVCP120D!std::locale::locale+0x2e [f:\dd\vctools\crt\crtw32\stdhpp\xlocale @ 324]
  01 000000c0`dcdbf450 000007fe`92454e25 MSVCP120D!std::ios_base::getloc+0x29 [f:\dd\vctools\crt\crtw32\stdhpp\xiosbase @ 424]
  *** WARNING: Unable to verify checksum for JustPlayC.exe
  02 000000c0`dcdbf490 000007f7`524c33c0 MSVCP120D!std::basic_ios&lt;char,std::char_traits&lt;char&gt; &gt;::widen+0x25 [f:\dd\vctools\crt\crtw32\stdhpp\ios @ 127]
  03 000000c0`dcdbf4f0 000007fe`9246570e JustPlayC!std::endl&lt;char,std::char_traits&lt;char&gt; &gt;+0x40 [c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream @ 999]
  04 000000c0`dcdbf520 000007f7`524c2cb1 MSVCP120D!std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::operator&lt;&lt;+0x2e [f:\dd\vctools\crt\crtw32\stdhpp\ostream @ 201]
  05 000000c0`dcdbf550 000007f7`524cefb1 JustPlayC!A::~A+0x41
  06 000000c0`dcdbf580 000007fd`edd558c3 JustPlayC!`dynamic atexit destructor for 'a''+0x21
  07 000000c0`dcdbf5b0 000007fd`edd551b3 MSVCR120D!doexit+0x133 [f:\dd\vctools\crt\crtw32\startup\crt0dat.c @ 628]
  08 000000c0`dcdbf620 000007fd`edd55aeb MSVCR120D!_cexit+0x13 [f:\dd\vctools\crt\crtw32\startup\crt0dat.c @ 449]
  09 000000c0`dcdbf650 000007fd`edd559c1 MSVCR120D!__CRTDLL_INIT+0x10b [f:\dd\vctools\crt\crtw32\dllstuff\crtlib.c @ 325]
  0a 000000c0`dcdbf6a0 000007fe`d916ecef MSVCR120D!_CRTDLL_INIT+0x31 [f:\dd\vctools\crt\crtw32\dllstuff\crtlib.c @ 215]
  0b 000000c0`dcdbf6d0 000007fe`d9180f6c ntdll!LdrpCallInitRoutine+0x3f
  0c 000000c0`dcdbf720 000007fe`d9180e0c ntdll!LdrShutdownProcess+0x142
  0d 000000c0`dcdbf830 000007fe`d612870f ntdll!RtlExitUserProcess+0xac
  0e 000000c0`dcdbf870 000007fe`d61286e7 KERNELBASE!DefaultHandler+0xf
  0f 000000c0`dcdbf8a0 000007fe`d8841842 KERNELBASE!CtrlRoutine+0x9b
  10 000000c0`dcdbf990 000007fe`d9174f45 KERNEL32!BaseThreadInitThunk+0x1a [d:\win8_gdr\base\win32\client\thread.c @ 65]
  11 000000c0`dcdbf9c0 00000000`00000000 ntdll!RtlUserThreadStart+0x1d
  </code></pre>
  
  <p>Can anyone help me understand what is going on? Looks like MSVCP locale gets uninitalized before global variable destructor which relies on locale? What is the proper way to get around this?</p>