2

I'm trying to debug a method which among other things, adds items to a list which is local to the method.

However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this.

Thanks.

Olumide
  • 5,397
  • 10
  • 55
  • 104
  • You will need to figure out the member (easily lost in underscores and cryptic names ...) and set it to the condition. – dirkgently Oct 28 '09 at 07:24
  • Sorry about that. I'm programming C++ in Visual Studio .NET 2003 – Olumide Oct 28 '09 at 07:24
  • you can (almost) always make the variable global and set a data breakpoint there – Rom Oct 28 '09 at 07:25
  • Suggestion: please edit the original question! I would also suggest changing the tags from "visual" and "studio" to one tag. – JXG Oct 28 '09 at 07:26
  • My first reaction to this was that its a threading problem - are you sure that a reference to your collection isnt passed elsewhere? – Justin Oct 28 '09 at 09:18
  • Interesting though Kragen. I'm not using threads tho -- at least not that I'm aware of. – Olumide Oct 28 '09 at 16:17

4 Answers4

5

Why not use conditional breakpoints?

http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx

Faruz
  • 9,909
  • 10
  • 48
  • 66
  • Thanks ... unfortunately, its not available in VS.NET 2003 :( – Olumide Oct 28 '09 at 07:32
  • If you have the ability to change your program then you are better off going with o.k.w's suggestion anyway. – Justin Oct 28 '09 at 09:16
  • @Kragen: Added C++ codes (no mistake I hope) to my answers. Though I don't think mine is the best solution. – o.k.w Oct 28 '09 at 09:29
3

in C#

if(theList.Count == 0){
  //do something meaningless here .e.g.
  int i = 1; //  << set your breakpoint here
}

in VB.NET

If theList.Count = 0 Then
  'do something meaningless here .e.g.
  Dim i = 1; '  << set your breakpoint here
End If

For completeness sake, here's the C++ version:

if(theList->Count == 0){
  //do something meaningless here .e.g.
  int i = 1; //  << set your breakpoint here
}
o.k.w
  • 25,490
  • 6
  • 66
  • 63
  • there is "stl" tag in the question: STL is a C++ thing – Rom Oct 28 '09 at 07:26
  • @Rom: You are right, I missed it, though Faruz's conditional breakpoint seems like a better option. – o.k.w Oct 28 '09 at 07:29
  • I've checked the size of the list at all the places where I access the list. The problem is that the list randomly gets cleared. I'm starting to suspect stack corruption. [example] list sizes: 0, 1, 2, 3, 4, 5, 6, 7, (0) , 1, 2, 3, (0) ... – Olumide Oct 28 '09 at 07:29
  • @Olumide: I suggest you 'watch' the list while debugging. – o.k.w Oct 28 '09 at 07:33
  • Watching works fine. Problem is that the the method loops a couple of thousand times. Watching a variable for that long is way too tedious that's why I would like to break as soon as a condition is encountered. – Olumide Oct 28 '09 at 07:42
  • @Olumide: Are there multiple threads? Seems like the list is 'accessed and modified' all over the place which makes it hard to trace huh? – o.k.w Oct 28 '09 at 07:52
0

I can give a partial answer for Visual Studio 2005. If you open the "Breakpoints" window (Alt + F9) you get a list of breakpoints. Right-click on the breakpoint you want, and choose "Condition." Then put in the condition you want.

JXG
  • 7,263
  • 7
  • 32
  • 63
  • I tried that too, breakpoint was flagged: "The breakpoint will not currently be hit. Unable to find variables in condition ...". I suspect VS.NET has problems with setting breakpoints on local variables. – Olumide Oct 28 '09 at 07:40
0

You have already got both major options suggested: 1. Conditional breakpoints 2. Code to check for the wrong value, and with a breakpoint if so happens

The first option is the easiest and best, but on large loops it is unfortunately really slow! If you loop 100's of thousands iterations the only real option is #2. In option #1 the cpu break into the debugger on each iteration, then it evaluates the condition and if the condition for breaking is false it just continiues execution of the program. This is slow when it happens thousands of times, it is actually slow if you loop just 1000 times (depending on hardware of course)

As I suspect you really want an "global" breakpoint condition that should break the program if a certain condition is met (array size == 0), unfortunately that does not exist to my knowledge. I have made a debugging function that checks the condition, and if it is true it does something meaningless that I have a breakpoint set to (i.e. option 2), then I call that function frequently where I suspect the original fails. When the system breaks you can use the call stack to identify the faulty location.

sindre j
  • 4,404
  • 5
  • 31
  • 32