简体   繁体   English

在局部变量值上设置Visual Studio(条件)断点

[英]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# 在C#中

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

in VB.NET 在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: 为了完整起见,这里是C ++版本:

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. 我可以为Visual Studio 2005提供部分答案。如果打开“断点”窗口(Alt + F9),您将获得断点列表。 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 您已经建议了两个主要选项:1。条件断点2.检查错误值的代码,如果发生这种情况,则使用断点

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. 如果你循环100次千次迭代,唯一真正的选择是#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. 在选项#1中,cpu在每次迭代时都会进入调试器,然后它会评估条件,如果break的条件为false,它只会继续执行程序。 This is slow when it happens thousands of times, it is actually slow if you loop just 1000 times (depending on hardware of course) 当它发生数千次时这很慢,如果你只循环1000次它实际上很慢(当然取决于硬件)

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. 因为我怀疑你真的想要一个“全局”断点条件,如果满足某个条件(数组大小== 0)就应该破坏程序,不幸的是,根据我的知识,这不存在。 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. 我已经制作了一个调试函数来检查条件,如果它是真的它做了一些没有意义的事情,我有一个断点设置(即选项2),然后我经常调用该函数,我怀疑原来的失败。 When the system breaks you can use the call stack to identify the faulty location. 当系统中断时,您可以使用调用堆栈来识别故障位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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