Set Visual Studio (conditional) breakpoint on local variable value

允我心安 提交于 2020-01-15 08:32:11

问题


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:


Why not use conditional breakpoints?

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




回答2:


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
}



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1635609/set-visual-studio-conditional-breakpoint-on-local-variable-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!