title: Visual Studio can't show the value of std::string
body: <p>At the company I am working at some of us have been affected by a bug/error in Visual Studio. The debugger fails to show the value/content of std::string. It says "Error reading characters of string."</p>
  
  <p>Some pics of the issue:
  <a href="https://i.stack.imgur.com/vFqE3.png" rel="nofollow noreferrer">Simple string vars</a>
  <a href="https://i.stack.imgur.com/BYFqg.png" rel="nofollow noreferrer">Values in debugger</a></p>
  
  <p>The following line will always reproduce the issue, but in general every string in the code (~1'000'000 lines at least) will have the issue.</p>
  
  <pre><code>std::string testVar = "Something";
  </code></pre>
  
  <p>Edit: Will try to make a Minimal Reproducible Example soon, but right now I am unable to make one. The problem only occours in a single solution consisting of 93 projects.
  Tried so far: New solution with same compiler version (v120), any compiler version, same preprocessor definitions, VS2013 and VS2017 (Professional and Enterprise both produce the issue). Now installing VS2019 to test it.</p>
  
  <p>I construct the string from a const char*, so this has probably nothing to do with memory allocations, etc... <strong>And most importantly, the code works.</strong> So, the std::string itself functions as it should, but the debugger can't show it's values.
  The size is correct as far as I can see, but the capacity is always 0xcccccccccccccccc.
  For the characters in the string is says: "Unable to read memory".
  <a href="https://i.stack.imgur.com/TGRbU.png" rel="nofollow noreferrer">Detailed view of string in debugger</a></p>
  
  <p>This issue only happens with std::string, not with std::wstring or char * or anything else.
  Also, when I open a new solution, std::string will show properly. It only fails to display in a solution with 91+ projects. Reproducable in both VS2017 and VS2013 but we are using the v120 compiler. Tried const variables and having the variable on heap or on stack, it doesn't matter.</p>
  
  <p>Also, it only happens in the debug build, in release mode the debugger works just fine.</p>
  
  <p>This error has been present since this week, we couldn't observe anything like this before, or even more than a week ago. 3 of us have this issue, but several others don't. We are all running mostly the same machines with the same config and updates. One difference is that the working VS is Professional, and we use the Enterprise version.</p>
  
  <p>I know this is not a serious issue as the code works, but this makes debugging a lot more difficult sometimes.</p>
  
  <p><strong>Edit:</strong>
  Might be important, that when debugging by stepping through instructions, the debugger won't step into the constructor or any of the std::string functions (in xstring), it will simply step over it as it were a simple int variable assignment, not a function call.  But setting a breakpoint in the source code of std::string will actually work, and the debugger will stop there.</p>
  
  <p><strong>EDIT2 - Found the reason of the bug, but no real solution yet</strong></p>
  
  <p>After analyzing the values of the pointers in the std::string, I found why the debugger fails to extract the string. The debugger looks for it with a +8 byte offset.
  To explain it: Let's say I create an std::string, and get it's pointer.</p>
  
  <pre><code>std::string testStr = "test";
  std::string* strPtr = &amp;testStr;
  </code></pre>
  
  <p>Then, strPtr = 0x0000000001bdf4a8.
  Now, when I look at the debugger's "Locals" window, and look up the Raw View of the string and find the pointer in the _Bx union, then:
  _Buf = 0x0000000001bdf4b0
  This is strPtr+8. That is why it fails to decode the content of the string. because it tries to interpret it at the wrong address.
  If I were to do this:</p>
  
  <pre><code>std::string* fixedStr = reinterpret_cast&lt;std::string*&gt;(reinterpret_cast&lt;char*&gt;(&amp;testStr)-8);
  </code></pre>
  
  <p>So, when I manually decrease the pointer by 8 bytes and recast it into a string, the debugger will properly display its contents. (Of course, this string is corrupt and won't work, only the debugger display is fixed)</p>
  
  <p>When I open a new solution and look into the string's addresses the same way, the debugger shows the same address for _Buf as the string's pointer, which is the normal behaviour. The difference between the two is that when it fails to decocde the string, std::_Container_base has the type std::_Container_base12 and has contents, and when it works, it has std::_Container_base0 and has zero contents. Those extra contents cause the extra 8 bytes probably.</p>
  
  <p>Related things I found but did not offer a solution (tried the methods here but the problem persisted):
  <a href="https://stackoverflow.com/questions/13680523/visual-c-2010-refuses-to-show-stdstring-value-when-debugging-shows-bad-ptr">Visual C++ 2010 refuses to show std::string value when debugging. Shows &lt;Bad Ptr&gt;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/58410767/visual-studio-debugger-doesnt-display-stdstring-properly-in-debug">Visual Studio debugger doesn&#39;t display std::string properly in debug</a></p>
  
  <p><a href="https://developercommunity.visualstudio.com/content/problem/782573/visual-studio-debugger-doesnt-display-stdstring-pr.html" rel="nofollow noreferrer">https://developercommunity.visualstudio.com/content/problem/782573/visual-studio-debugger-doesnt-display-stdstring-pr.html</a></p>
  
  <p>Also tried to mess around with natvis according to this:
  <a href="https://stackoverflow.com/questions/40216882/how-to-visualise-a-simple-stdstring-with-natvis">How to visualise a simple std::string with natvis?</a></p>