title: Does the C++ static initialization fiasco apply to class hierarchy?
body: <p>I'm getting a crash (debug assertion failure: invalid CRT heap pointer in VC++ 2008) in static initialization, and I'm not sure I understand why.</p>
  
  <p>I've read all about the <a href="http://www.parashift.com/c++-faq/static-init-order.html" rel="nofollow" title="static initialization order fiasco">static initialization fiasco</a> over at the <a href="http://www.parashift.com/c++-faq/index.html" rel="nofollow" title="C++ FAQ">C++ FAQ</a>, and I thought I had a grasp on it--I don't understand why this is happening, or why it would be a case of the fiasco.</p>
  
  <p>So here's the situation (most non-static members are omitted for the sake of brevity).  I have one class, A, defined in A.h:</p>
  
  <pre><code>class A {
  public:
      virtual ~A() { }
  
      virtual void do_something();
  };
  </code></pre>
  
  <p>Then, I have a class, C, which has a nested class, B, which is a subclass of A.  C also contains a private static member of type B:</p>
  
  <pre><code>class C {
  public:
      void do_the_C_thing();
  
  private:
      class B : public A {
      public:
          virtual void do_something();
      };
  
      static B my_personal_B;
  };
  </code></pre>
  
  <p>Finally, there's C's implementation file, C.cpp, which contains my_personal_B's storage unit:</p>
  
  <pre><code>C::B my_personal_B;
  
  C::C() {
  }
  
  C::do_the_C_thing() {
      // [...]
      my_personal_B.do_something();
      // [...]
  }
  
  void C::B::do_something() {
      // overridden do_something for C's private B class
  }
  </code></pre>
  
  <p>This pattern is repeated for a lot of classes, each having a nested class which inherits from A.  This has all been operating flawlessly through several code revisions, but lately the application crashes with this specific error message:</p>
  
  <blockquote>
    <p>Debug Assertion Failed! <br>
    <br>
    Program: <br>
    [redacted].exe<br>
    File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c<br>
    Line: 1511<br>
    <br>
    Expression: _CrtIsValidHeapPointer(pUserData)<br></p>
  </blockquote>
  
  <p>If I click to debug, I'm shown the line in C.cpp, where the static member is defined.</p>
  
  <p>This doesn't seem like a static fiasco, because nothing static refers to my_personal_B, neither A nor B have anything but default constructors, and therefore couldn't possibly be referring to some other not-yet-initialized static object.  The way I understood the fiasco is that it happened when one static object referred to another not-yet-initialized static object.</p>
  
  <p>Nevertheless, if I change the static member to an initialize-on-first-use method, the crash seems to go away.  </p>
  
  <p>So the question is, why is this crashing?</p>