Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a breakpoint for when a vector<int> increases in length?

I have a vector that is empty, something is filling it somewhere and I cant find it. I want to set a memory breakpoint so that when the push_back occurs the program will break.

I'm using Visual Studio 2008, and the problem is that the vector doesnt display its internal members in the watch window (it seems to have a custom formatting). It just looks like this:

myVector[0]() std::vector< int,std::allocator< int > >

Indicating size 0. Any tips?

like image 309
baddie Avatar asked Jul 07 '11 22:07

baddie


1 Answers

This answer is for VS 2010 - details might differ in other versions.

Since the debugger uses a data visualizer (or whatever it's called) to display the state of a std::vector, you have to look in the <vector> header to determine the names of the actual members of the class and which one(s) might be responsible for tracking the number of elements. An easy way to do this is to step through a call to vector::push_back().

In VC++ 2010 this is a member pointer named _Mylast.

So all you have to do is set a data breakpoint on writes to &v._Mylast (where v is the vector you are interested in debugging).

The next time an element is added, the debugger will break with a call stack showing exactly where.

like image 157
Michael Burr Avatar answered Oct 11 '22 03:10

Michael Burr