I have a stack-allocated std::vector
that is being overwritten by some rogue code. It is not the data being overwritten, but the internal state. I know this because the size()
member function returns a junk value some time into the program. It initialises correctly. I suspect a common pointer bug elsewhere in my code.
I am using Xcode 4.6.2. I want to set a hardware breakpoint (with lldb) upon memory access to the vector's first memory location (of the vector itself, not the data), so I can see what is overwriting it. According to this I need to first find the address of the vector. Normally one would use the &
operator to get the address of a variable, but for some reason with lldb this doesn't return the address but instead prints some sort of summary string.
class myClass {
public:
myClass() : myVector(4) {}
private:
std::vector<double> myVector;
double myDouble;
};
After breaking at an arbitrary breakpoint after everything is constructed:
(lldb) frame variable &myObject.myVector
(std::vector<double, std::allocator<double> > *) $7 = size=4
'expr' has the same result:
(lldb) expr &myObject.myVector;
(std::vector<double, std::allocator<double> > *) $8 = size=4
Normally I'd expect to see the address printed, such as with this plain data member:
(lldb) frame variable &myObject.myDouble
(double *) &myDouble = 0x0000000125589328
I tried assigning the address to a variable with 'expr' but that doesn't work either:
(lldb) expr std::vector<double, std::allocator<double> > * f = &myObject.myVector; f
(std::vector<double, std::allocator<double> > *) $12 = size=0
The zero size (instead of 4) returned there suggests it didn't actually pick up the right vector anyway.
So how do I get the address of this vector? If I right-click on it in Xcode's list of frame variables, and choose 'View Memory' of this vector, then it opens a view of 0x0
which of course is invalid. Note that the vector is allocated on the stack - it's actually inside a few other objects but all of these are stack constructed.
Please note that I am not wanting to obtain the address of the data within the vector - this isn't actually being overwritten. It is the internal storage of the stack-allocated vector object that is being damaged.