title: Debug Assertion Failed! Expression: _pFirstBlock == pHead
body: <p>I am calling into a statically linked .dll, and I see this error:</p>
  
  <p><img src="https://i.stack.imgur.com/VFqPg.png" alt="enter image description here"></p>
  
  <p>I wrote both the .dll and the calling code. This error should not be occurring. I am wondering if anyone else has encountered it before? The .dll only contains about 10 lines of code, its just a test .dll to see how dlls work in general. It blows up when I pass a std::string back out of the .dll.</p>
  
  <p>I am using Visual Studio 2012 and C++.</p>
  
  <p><strong>What I will try next</strong></p>
  
  <p>From <a href="http://computer-programming-forum.com/77-vc-atl/8efc3907d1099f6b.htm" rel="noreferrer">Debug assertion... _pFirstBlock == pHead</a>:</p>
  
  <blockquote>
    <p>This problem can occur if one uses the single-threading libraries in a
    multithreaded module.</p>
  </blockquote>
  
  <p>Tomorrow, I'll try recompiling the Boost static libraries in multi-threaded mode (my .dll is set to multi-threaded static mode).</p>
  
  <p><strong>What I will try next</strong></p>
  
  <p>See <a href="http://computer-programming-forum.com/84-vc-stl/858c3f0cc87cb098.htm" rel="noreferrer">Using strings in an object exported from a DLL causes runtime error</a>:</p>
  
  <blockquote>
    <p>You need to do one of two things </p>
    
    <ol>
    <li>Make both the DLL and the client that use it both link to the <em>DLL</em>  version of the CRT (e.g. not statically). </li>
    <li>OR You need to make sure you don't pass dynamically allocated  memory (such as is contained in string objects) across DLL boundaries.
    In other words, don't have DLL-exported functions that return string 
    objects. </li>
    </ol>
    
    <p>Joe</p>
  </blockquote>
  
  <p>This seems to match whats going on, it blows up at the precise point where I pass a string back across a .dll boundary. The problem only occurs when everything is linked in static mode. Now that's fixable.</p>
  
  <p>See <a href="https://stackoverflow.com/questions/17885060/passing-reference-to-stl-vector-over-dll-boundary">Passing reference to STL vector over dll boundary</a>.</p>
  
  <p><strong>What I will try next</strong></p>
  
  <p>See <a href="https://stackoverflow.com/questions/15177907/unable-to-pass-stdwstring-across-dll">Unable to pass std::wstring across DLL</a>.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I have a nice solution, see the answer below.</p>