views:

542

answers:

4

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.

+1  A: 

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
there is "stl" tag in the question: STL is a C++ thing
Rom
@Rom: You are right, I missed it, though Faruz's conditional breakpoint seems like a better option.
o.k.w
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
@Olumide: I suggest you 'watch' the list while debugging.
o.k.w
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
@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
+2  A: 

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
Thanks ... unfortunately, its not available in VS.NET 2003 :(
Olumide
Oh, didn't know you're working on 2003.
Faruz
If you have the ability to change your program then you are better off going with o.k.w's suggestion anyway.
Kragen
@Kragen: Added C++ codes (no mistake I hope) to my answers. Though I don't think mine is the best solution.
o.k.w
A: 

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
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
A: 

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