title: "Magic static" singleton crashing when referenced in static destruction phase of another translation unit
body: <p>I have a trivial singleton class. My <code>singleton.h</code> file looks something like this:</p>
  
  <pre><code>class singleton
  {
  ...
  public:
      static singleton&amp; instance();
  };
  </code></pre>
  
  <p>And my <code>singleton.cpp</code> looks like this:</p>
  
  <pre><code>...
  singleton&amp; singleton::instance()
  {
      static singleton * const _instance(new singleton);
      return *_instance;
  }
  </code></pre>
  
  <p>In writing this class, I thought I was relying on thread-safe function-local static initialization, which I understand to be set out in section 6.7 of the C++ standard as described <a href="https://stackoverflow.com/a/8102145/1911388">here</a>. Hopefully I understand how this is supposed to work.</p>
  
  <p>I'm running Visual C++ with the <a href="http://blogs.msdn.com/b/vcblog/archive/2013/11/18/announcing-the-visual-c-compiler-november-2013-ctp.aspx" rel="nofollow noreferrer">November 2013 CTP</a> toolchain. Microsoft says November 2013 CTP supports thread-safe function-local static initialization and a quick glance at object code produced by the compiler shows it is trying to do so.</p>
  
  <p>My problem is that the destruction of a static storage duration object in another translation unit requires access to <code>singleton::instance()</code>. I expected this would not present any difficulty because the static variable backing <code>singleton::instance()</code> is a pointer that is never deleted. However, calls to <code>singleton::instance()</code> from that other object are crashing my process and the stack trace looks like this:</p>
  
  <pre><code>_Init_thread_header
  singleton::instance
  other_translation_unit_object::~other_translation_unit_object
  </code></pre>
  
  <p>Where <code>_Init_thread_header()</code> appears to be inserted by the compiler to implement thread-safe static initialization.</p>
  
  <p>So my question is: does the code above reveal that I'm fundamentally misunderstanding how static initialization is supposed to work (most likely case, so be nice if so :), or is it possible something else is awry?</p>