LLDB有时会显示矢量数据,而其他时间则不会

时间:2017-12-17 05:40:22

标签: c++ xcode vector xcode9 lldb

在大多数情况下,在调试时,如果我有一个向量(在Xcode 9中),我会看到一个表示向量中值的索引列表。

所需 enter image description here

其他时候,我得到了这个无益的代表:

不期望的 enter image description here

我无法弄清楚哪种情况会导致LLDB以不合需要的方式显示向量。

问题
是什么导致了不良行为?可以在不重写代码的情况下修复吗?这是LLDB中的错误吗?

这是一个重现不良行为的简短代码示例:

#include <iostream>
#include <vector>

std::vector<int> createVector()
{
    std::vector<int> v = { 1, 2, 3 };
    return v;
}

int main(int argc, const char * argv[])
{
    const auto& v = createVector();
    std::cout << v.front() << std::endl;
    return 0;
}


这是Xcode项目的链接:
http://s000.tinyupload.com/?file_id=21020556485232357417

2 个答案:

答案 0 :(得分:3)

抱歉,我在答案中添加了我的评论,因为Stack Overflow评论不支持格式化。

这绝对是lldb的问题。您的图片不包含完整的v说明:

v = (const std::__1::vector<int, std::__1::allocator<int> > &) size=1

size=1不正确。 除了lldb控制台中的print命令正确打印v

(lldb) p v
(const std::__1::vector<int, std::__1::allocator<int> >) $1 = size=3 {
  [0] = 1
  [1] = 2
  [2] = 3
}

似乎Xcode使用lldb frame var命令来显示变量。这将显示Xcode显示的完全相同的输出:

(lldb) frame variable -T
(int) argc = 1
(const char **) argv = 0x00007fff5fbff710
(const std::__1::vector<int, std::__1::allocator<int> > &) v = size=1: {
  (std::__1::__vector_base<int, std::__1::allocator<int> >) std::__1::__vector_base<int, std::__1::allocator<int> > = {
    (std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __begin_ = 0x000000010103fa40
    (std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __end_ = 0x000000010103fa4c
    (std::__1::__compressed_pair<int *, std::__1::allocator<int> >) __end_cap_ = {
      (std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2>) std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2> = {
        (int *) __first_ = 0x000000010103fa4c
      }
    }
  }
}

我认为这个问题来自于变量v最初是在另一个堆栈帧中创建并初始化的事实,因此当初始向量作为一个传递时,下层堆栈帧中有关该向量的一些信息是未知的。函数调用的结果。

答案 1 :(得分:3)

这是std :: vector数据摘要&amp;格式化程序适用于参考变量。请注意,在expr v中,表达式解析器实际上认为v是一个直向量,而不是对向量的引用......这就是打印工作的原因。

相关问题