c++ - Visual Studio 2013 调试器显示 std::string 的奇怪值

标签 c++ debugging visual-studio-2013 visual-studio-debugging

我有一个大型 cmake 生成的解决方案,其中包含许多项目。出于某种原因,我无法查看字符串的内容,因为根据调试器 _Bx._Buf 包含一些垃圾。

text.c_str() 正确返回 "Hello"

问题不仅发生在本地字符串上。返回 std::string 的函数在调试器中似乎也是垃圾,而实际上它们是正常的。

为清楚起见:屏幕截图是在执行分配行之后制作的。所以不是文本未初始化。

另一个信息:如果我在 visual studio 中创建一个新的控制台项目,那么它工作正常我可以看到任何字符串的内容。只有这个 cmake 生成的项目我有问题。

项目属性的字符集设置为“使用多字节字符集”

调试器类型设置为“自动”,但我也尝试过“混合”和“仅限 native ”,但都是一样的。我看不到字符串。

有没有人知道是什么设置导致了这种行为?

enter image description here

最佳答案

一个可能的原因是您正在使用的 STL 版本(在他的 CMake 项目中)与他们拥有的 STL.Natvis 不匹配。在我的 VS 2013 (Microsoft Visual Studio 12.0\Common7\Packages\Debugger\Visualizers\STL.natvis) 上,basic_string 有两个 natvis 条目:

<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>

<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>

由于在将 STL 显示字符串输入监 window 口期间可能会获得相同的值,因此您可能需要不同的 STL.natvis 才能使 std::string 正常工作。您应该尝试进入 text.c_str() 以查看它实际返回的内容。感觉是比较新的STL。

关于c++ - Visual Studio 2013 调试器显示 std::string 的奇怪值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868715/

相关文章:

c# - 计算 dataGridView 中选定行的值

c++ - 如何诊断 Windows 上的堆损坏错误?

c++ - C++ 程序如何为插入 (<<) 运算符选择正确的函数?

c++ - 带有 libcurl 的 HTTP Post header 字段

c - 如何在 C 中访问 ruby​​ 数组的值? (编辑 array.c :rb_ary_initialize from ruby source code)

.net - 如何在多进程系统上使用单个proc进行调试?

c++ - 无限图算法

c++ - 由于 next_permute 而运行无限循环

qt - QGraphicsScene::clear() 清除场景但不清除 View

c - 从命令行使用 Visual Studio 2013 编译 aritchk.c 时出现未解析的符号