问题
I have this C++11 code that uses unique_ptr
in vector.
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class A
{
int x;
public:
A() {}
~A() {}
A(A& a) {}
A(int x) {this->x = x;}
int get() {return x;}
};
int main()
{
vector<unique_ptr<A>> v;
auto a = new A(10);
unique_ptr<A> pa(a);
v.push_back(move(pa)); // move(pa);
auto a2 = new A(20);
unique_ptr<A> pb(a2);
v.push_back(move(pb)); // move(pa);
for (auto& i: v)
{
cout << i->get();
}
}
I could use Xcode to debug and check value in vector>. However, when I compile the same code with clang and debug in lldb, there are two errors.

The first one is lldb traces into STL source code.
* thread #1: tid = 0x1f03, 0x0000000100001744 a.out`__gnu_cxx::__normal_iterator<std::unique_ptr<A, std::default_delete<A> >*, std::vector<std::unique_ptr<A, std::default_delete<A> >, std::allocator<std::unique_ptr<A, std::default_delete<A> > > > >::operator*() const at stl_iterator.h:740, stop reason = step over
frame #0: 0x0000000100001744 a.out`__gnu_cxx::__normal_iterator<std::unique_ptr<A, std::default_delete<A> >*, std::vector<std::unique_ptr<A, std::default_delete<A> >, std::allocator<std::unique_ptr<A, std::default_delete<A> > > > >::operator*() const at stl_iterator.h:740
737
738 // Forward iterator requirements
739 reference
-> 740 operator*() const
741 { return *_M_current; }
742
743 pointer
(lldb) n
Process 41243 stopped
The second error is I have segmentation error when I tried to see the vector contents.
(lldb) p v
Segmentation fault: 11
I used this command for compilation.
clang++ -std=c++11 -stdlib=libc++ -g testit.cpp -o a
What might be wrong?
回答1:
I reinstalled the Xcode with the newest version for Lion: Xcode 4.6, and the problem is gone.
回答2:
your segmentation fault seems like it could be caused by one of two things:
- the LLDB expression parser
- the LLDB data formatters
In order to tell which is which, the crash log will be necessary.
If you could retrieve it from ~/Library/Logs/DiagnosticReports
and append it to this question (and also file a bug with Apple at http://bugreport.apple.com) it would be a good starting point
Also, it looks like you are using a pre-5 Xcode. Is that correct? Do you have access to the Xcode 5 Developer Preview to test if this reproduces? LLDB has been vastly improved between 4.x and 5, so you might have better luck.
来源:https://stackoverflow.com/questions/17256800/lldb-on-xcode-vs-lldb-on-standalone