Yeah I'm completely baffled. Test code, with class names changed:
Code:
void ClassX::method() {
MyClass inst;
cout << inst.nodes.size() << endl;
MyClass::test();
}
void MyClass::test() {
MyClass sharedInstance;
cout << sharedInstance.nodes.size() << endl;
}
Debugger session:
Code:
// From ClassX::method()
// Stepped into vector constructor after it initializes __begin_ and __end_ to nullptr.
(lldb) p &__begin_
(pointer *) $0 = 0x27de8bbc
(lldb) p &__end_
(pointer *) $1 = 0x27de8bc0
// Breakpoint just after constructor returns
(lldb) p &(inst.nodes.__begin_)
(pointer *) $4 = 0x27de8bbc
(lldb) p &(inst.nodes.__end_)
(pointer *) $5 = 0x27de8bc0
// Cout of inst.nodes.size()
0
// From MyClass::test() in same session immediately afterwards
// Stepped into vector constructor after it initializes __begin_ and __end_ to nullptr.
(lldb) p &__begin_
(pointer *) $6 = 0x27de8a80
(lldb) p &__end_
(pointer *) $7 = 0x27de8a84
// Breakpoint just after constructor returns
(lldb) p &(sharedInstance.nodes.__begin_)
(pointer *) $10 = 0x27de8a98 // Wrong address?
(lldb) p &(sharedInstance.nodes.__end_)
(pointer *) $11 = 0x27de8a9c // Wrong address?
// cout of sharedInstance.nodes.size()
-146087323 // obviously wrong
(lldb) p sharedInstance.nodes.__end_
(std::__1::__vector_base<GLNode, std::__1::allocator<GLNode> >::pointer) $13 = 0x27de8ae4
(lldb) p sharedInstance.nodes.__begin_
(std::__1::__vector_base<GLNode, std::__1::allocator<GLNode> >::pointer) $14 = 0x27de8ae8
(lldb) p ((int)sharedInstance.nodes.__end_ - (int)sharedInstance.nodes.__begin_)
(int) $16 = -4 // Different from .size()
(lldb) p *(int**)0x27de8a80 // Original address of __begin_
(int *) $17 = 0x00000000 // as originally initialized in constructor
(lldb) p *(int**)0x27de8a84 // Original address of __end_
(int *) $18 = 0x00000000 // as originally initialized in constructor
The std::vector implementation has .size() as:
Code:
size_type size() const _NOEXCEPT
{return static_cast<size_type>(this->__end_ - this->__begin_);}
I would have assumed that the debugger was just somehow getting the wrong address for .nodes.__begin_ and __end_, but I don't know where .size() is getting its value from - and it doesn't match what I calculate from the values at the 'wrong' addresses either.