Visual Studio 2013 debugger showing weird values for std::string

泄露秘密 提交于 2019-12-13 16:12:29

问题


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&lt;char,*&gt;">
    <DisplayString Condition="_Myres &lt; _BUF_SIZE">{_Bx._Buf,s}</DisplayString>
    <DisplayString Condition="_Myres &gt;= _BUF_SIZE">{_Bx._Ptr,s}</DisplayString>
    <StringView Condition="_Myres &lt; _BUF_SIZE">_Bx._Buf,s</StringView>
    <StringView Condition="_Myres &gt;= _BUF_SIZE">_Bx._Ptr,s</StringView>
    <Expand>
        <Item Name="[size]">_Mysize</Item>
        <Item Name="[capacity]">_Myres</Item>
        <ArrayItems>
            <Size>_Mysize</Size>
            <ValuePointer Condition="_Myres &lt; _BUF_SIZE">_Bx._Buf</ValuePointer>
            <ValuePointer Condition="_Myres &gt;= _BUF_SIZE">_Bx._Ptr</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>

And

<Type Name="std::basic_string&lt;unsigned short,*&gt;">
    <AlternativeType Name="std::basic_string&lt;wchar_t,*&gt;" />
    <DisplayString Condition="_Myres &lt; _BUF_SIZE">{_Bx._Buf,su}</DisplayString>
    <DisplayString Condition="_Myres &gt;= _BUF_SIZE">{_Bx._Ptr,su}</DisplayString>
    <StringView Condition="_Myres &lt; _BUF_SIZE">_Bx._Buf,su</StringView>
    <StringView Condition="_Myres &gt;= _BUF_SIZE">_Bx._Ptr,su</StringView>
    <Expand>
        <Item Name="[size]">_Mysize</Item>
        <Item Name="[capacity]">_Myres</Item>
        <ArrayItems>
            <Size>_Mysize</Size>
            <ValuePointer Condition="_Myres &lt; _BUF_SIZE">_Bx._Buf</ValuePointer>
            <ValuePointer Condition="_Myres &gt;= _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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!