问题
I have a large cmake generated solution with many projects in it. For some reason I cannot view the contents of a string because according to the debugger _Bx._Buf
contains some garbage.
text.c_str()
returns correctly "Hello"
.
The problem does not only occur for local strings. Functions that return std::string
also appear to be garbage in debugger whereas in reality they are OK.
For clarity: the screenshot was made after the assignment line was executed. So it is not that text is uninitialized.
Another info: If I create a new console project in visual studio then it works fine I can see the contents of any string. It is only this cmake generated project I have issues with.
The Character Set of the project properties is set to "Use Multi-Byte Character Set"
Debugger Type is set to "Auto" but I have tried "Mixed" and "native Only" too but it is all the same. I can't see strings.
Does anyone have a clue what setting causes this behaviour?
回答1:
One possible reason is that the version of STL that you are using (in his CMake project) doesn’t match the STL.Natvis they have. On my VS 2013 (Microsoft Visual Studio 12.0\Common7\Packages\Debugger\Visualizers\stl.natvis), basic_string has two natvis entries:
<Type Name="std::basic_string<char,*>">
<DisplayString Condition="_Myres < _BUF_SIZE">{_Bx._Buf,s}</DisplayString>
<DisplayString Condition="_Myres >= _BUF_SIZE">{_Bx._Ptr,s}</DisplayString>
<StringView Condition="_Myres < _BUF_SIZE">_Bx._Buf,s</StringView>
<StringView Condition="_Myres >= _BUF_SIZE">_Bx._Ptr,s</StringView>
<Expand>
<Item Name="[size]">_Mysize</Item>
<Item Name="[capacity]">_Myres</Item>
<ArrayItems>
<Size>_Mysize</Size>
<ValuePointer Condition="_Myres < _BUF_SIZE">_Bx._Buf</ValuePointer>
<ValuePointer Condition="_Myres >= _BUF_SIZE">_Bx._Ptr</ValuePointer>
</ArrayItems>
</Expand>
</Type>
And
<Type Name="std::basic_string<unsigned short,*>">
<AlternativeType Name="std::basic_string<wchar_t,*>" />
<DisplayString Condition="_Myres < _BUF_SIZE">{_Bx._Buf,su}</DisplayString>
<DisplayString Condition="_Myres >= _BUF_SIZE">{_Bx._Ptr,su}</DisplayString>
<StringView Condition="_Myres < _BUF_SIZE">_Bx._Buf,su</StringView>
<StringView Condition="_Myres >= _BUF_SIZE">_Bx._Ptr,su</StringView>
<Expand>
<Item Name="[size]">_Mysize</Item>
<Item Name="[capacity]">_Myres</Item>
<ArrayItems>
<Size>_Mysize</Size>
<ValuePointer Condition="_Myres < _BUF_SIZE">_Bx._Buf</ValuePointer>
<ValuePointer Condition="_Myres >= _BUF_SIZE">_Bx._Ptr</ValuePointer>
</ArrayItems>
</Expand>
</Type>
Since you could get the same value during entering the STL display string into the watch window, you may need a different STL.natvis for your std::string for it to work. You should try stepping into text.c_str() to see what it is actually returning. I feel that it’s a newer STL.
来源:https://stackoverflow.com/questions/43868715/visual-studio-2013-debugger-showing-weird-values-for-stdstring