title: Strange behaviour when using C++17 static inline members in Visual Studio
body: <p>Yesterday I asked a question about this problem, but I wasn't able to give a MVCE. I've managed to reproduce this with a simple program. The problem is with using an std::list as a static inline declaration in a class. Microsoft Visual Studio does support this new C++17 feature. It had some bugs as of March, but as far as I know they've been fixed since. Here are instructions of how I can get this problem, this happens in debug mode.</p>
  
  <p>In <em>main.cpp</em></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "header1.h"
  
  int main()
  {
      return 0;
  }
  </code></pre>
  
  <p>In <em>header1.h:</em></p>
  
  <pre><code>#include &lt;list&gt;
  
  struct Boo
  {
      static inline std::list&lt;int&gt; mylist;
  };
  </code></pre>
  
  <p>In <em>anotherCPP.cpp</em></p>
  
  <pre><code>#include "Header1.h"
  </code></pre>
  
  <p>When the program exits main() it destroys all the static objects and throws an exception.</p>
  
  <p>If this doesn't crash, maybe on your system the compiler/linker optimised some code out, so you can try making <em>main.cpp</em> and <em>anotherCPP.cpp</em> do something. In <em>anotherCPP.cpp</em>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Header1.h"
  
  void aFunction()
  {
      std::cout &lt;&lt; Boo::mylist.size();
  }
  </code></pre>
  
  <p>And make main.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Header1.h"
  
  void aFunction();
  
  int main()
  {
      std::cout &lt;&lt; Boo::mylist.size();
      afunction();
  
      return 0;
  }
  </code></pre>
  
  <p>When the program exits I get an exception here when the std::list is being cleared. Here is the Visual Studio debug code where it crashes:</p>
  
  <pre><code>for (_Nodeptr _Pnext; _Pnode != this-&gt;_Myhead(); _Pnode = _Pnext)
  {   // delete an element
      _Pnext = _Pnode-&gt;_Next; // Here: Exception thrown: 
                              // read access violation.
                              // _Pnode was 0xFFFFFFFFFFFFFFFF.
  
      this-&gt;_Freenode(_Pnode);
  }
  </code></pre>
  
  <p>This happens only if I declare the static inline std::list&lt; int > mylist in the class. If I declare it as <em>static std::list&lt; int > mylist</em> in my class and then define it separately in one .cpp as <em>std::list&lt; int > Boo::mylist;</em> it works fine. This problem arises when I declare the std::list static inline and I include the header for the class in two .cpp files.</p>
  
  <p>In my project I have stepped through the std::list clear loop from above, I took note of the "this" pointer address. I stepped through the loop as it freed nodes in my list. It then came back to free other std::lists, including in std::unordered_map (as they also use std::lists from the looks of it). Finally when the read access exception is thrown and <em>_Pnode</em> is an invalid pointer address, I noticed the "this" pointer address is the same as the "this" pointer address when clearing <em>std::list&lt; int > mylist</em>, which makes me think that it's trying to delete it twice, and probably why it's crashing. </p>
  
  <p>I hope someone can reproduce this, I'm not sure what this is, if it's a bug or something I'm doing wrong. Also this happens for me in 32 and 64 bit, but only in debug mode, because the node freeing loop I provided is under a macro:</p>
  
  <pre><code>#if _ITERATOR_DEBUG_LEVEL == 2
  </code></pre>