简体   繁体   中英

Set Visual Studio (conditional) breakpoint on local variable value

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.

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
}

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.

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM