【问题标题】:LLDB: printing a vector referenced by a shared_ptrLLDB:打印由 shared_ptr 引用的向量
【发布时间】:2013-12-17 14:26:25
【问题描述】:

在我的代码中有这样的内容:

shared_ptr<vector<unsigned int>> f = 
    make_shared<vector<unsigned int>>();

我怎样才能漂亮地打印我只能访问 shared_ptr 对象的向量

 frame variable f

 frame variable f.__ptr_->size()

 call to a function 'std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::size() const' that is not present in the target

收到此错误?

【问题讨论】:

    标签: c++ debugging shared-ptr lldb


    【解决方案1】:

    鉴于此代码 sn-p:

    #include <vector>
    #include <memory>
    
    using namespace std;
    
    int main()
    {
        shared_ptr<vector<unsigned int> > f = 
            make_shared<vector<unsigned int> >();
        f->push_back(1);
        f->push_back(1);
        f->push_back(1);
        return 0;
    }
    

    LLDB 只适合我:

    (lldb) fr var
    (std::__1::shared_ptr<std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > >) f = 0x00000001001038c8 size=3 (strong=1 weak=1) {
      __ptr_ = 0x00000001001038c8 size=3
    }
    

    如果我展开 __ptr_,那就更好了:

    (lldb) fr var --ptr-depth=2 
    (std::__1::shared_ptr<std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > >) f = 0x00000001001038c8 size=3 (strong=1 weak=1) {   __ptr_ = 0x00000001001038c8 size=3 {
        [0] = 1
        [1] = 1
        [2] = 1   } }
    

    【讨论】:

    • 您使用的是哪个版本的 lldb?
    • Enrico 的示例适用于我的 Xcode 5.0 (lldb-300.2.53)。
    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 2014-08-06
    • 2016-07-10
    • 1970-01-01
    • 2015-10-03
    • 2019-06-15
    • 2015-05-15
    • 1970-01-01
    相关资源
    最近更新 更多