A method I work with that is called tens of thousands of times started throwing exceptions recently. In most debugging circumstances I would set a breakpoint at the top of this method and run through until I reach the call I'm interested in with a parameter value that triggers the exception. In this case that would be impractical, so I tried setting a breakpoint with a condition that will only break when that parameter value appears. I created a breakpoint at the position indicated below and gave it a condition str == "OffendingValue"
.
我使用的方法被称为数万次,最近开始抛出异常。在大多数调试环境中,我会在此方法的顶部设置一个断点并运行直到我通过触发异常的参数值到达我感兴趣的调用。在这种情况下,这是不切实际的,所以我尝试设置一个断点,其条件只有在出现该参数值时才会中断。我在下面指出的位置创建了一个断点,并给它一个条件str ==“OffendingValue”。
class Foo
{
// Bar() is called many, many times
void Bar(string str)
{
try
{
// Breakpoint inserted here
...
}
catch (Exception ex)
{
...
}
}
}
To my surprise, doing this caused Visual Studio and my application to stop functioning in Debug mode. My application started and output some simple logging messages but then stopped responding entirely. Thinking Visual Studio might just be performing a little slower due to the extra work it has to do to monitor the breakpoint condition, I stepped away from my desk for 15 mintues to give it some time to run. When I returned there was no change. I can reproduce the condition by deleting the breakpoint and recreating it with the same condition. Strangest of all, the Break All debugging command, which will usually break program execution on the statement that's currently execting whether it's a breakpoint or not, does nothing at all when I have this problematic breakpoint enabled.
令我惊讶的是,这样做会导致Visual Studio和我的应用程序在调试模式下停止运行。我的应用程序启动并输出一些简单的日志消息,然后完全停止响应。思考Visual Studio可能只是执行速度稍慢,因为它需要做额外的工作来监控断点情况,我离开办公桌15分钟就给它一些时间来运行。当我回来时,没有任何变化。我可以通过删除断点并使用相同的条件重新创建它来重现该条件。最奇怪的是,Break All debugging命令通常会破坏当前正在执行的语句上的程序执行是否为断点,当我启用了这个有问题的断点时,它根本不执行任何操作。
Has anyone encountered similar behavior with Visual Studio breakpoint conditions? I am able to use Hit Count conditions without problem.
有没有人遇到过与Visual Studio断点条件类似的行为?我可以毫无问题地使用命中计数条件。
3 个解决方案
#1
7
Anytime that I have tried to use conditional breakpoints in Visual Studio I've had the same problem. The debugger runs so slowly that it becomes useless. Instead I end up temporarily adding an if statement to the code and adding my breakpoint inside of that. This isn't as convenient, but the code executes at a normal pace and it does get the job done.
任何时候我试图在Visual Studio中使用条件断点我都遇到了同样的问题。调试器运行得如此之慢,以至于变得无用。相反,我最终临时在代码中添加了一个if语句,并在其中添加了断点。这不方便,但代码以正常速度执行,它确实完成了工作。
class Foo
{
// Bar() is called many, many times
void Bar(string str)
{
try
{
if(str == "condition")
{
int i = 0; // Breakpoint inserted here
}
...
}
catch (Exception ex)
{
...
}
}
}
#2
2
If you know what the offending value is, can you not just write a unit test for that method and debug it that way?
如果您知道有什么问题,那么您是否可以为该方法编写单元测试并以这种方式进行调试?
If not, if you know the exception, you could set up your debugger to break when that exception is thrown. Go to Debug | Exceptions and check Thrown for the exception in question.
如果没有,如果您知道异常,则可以将调试器设置为在抛出异常时中断。转到Debug |例外和检查抛出有问题的例外。
#3
0
I wonder if you are getting a stack overflow. Does VS track all the values for str or anything to do with each state of Bar? If so, the thousands of copies might add up.
我想知道你是否有堆栈溢出。 VS是否跟踪str的所有值或任何与Bar的每个状态有关的值?如果是这样,可能会增加数千份副本。
I wonder if you could eliminate the problem monitoring the value via a global variable instead, rather than one within the function.
我想知道你是否可以通过全局变量来消除监视值的问题,而不是函数中的一个。
#1
7
Anytime that I have tried to use conditional breakpoints in Visual Studio I've had the same problem. The debugger runs so slowly that it becomes useless. Instead I end up temporarily adding an if statement to the code and adding my breakpoint inside of that. This isn't as convenient, but the code executes at a normal pace and it does get the job done.
任何时候我试图在Visual Studio中使用条件断点我都遇到了同样的问题。调试器运行得如此之慢,以至于变得无用。相反,我最终临时在代码中添加了一个if语句,并在其中添加了断点。这不方便,但代码以正常速度执行,它确实完成了工作。
class Foo
{
// Bar() is called many, many times
void Bar(string str)
{
try
{
if(str == "condition")
{
int i = 0; // Breakpoint inserted here
}
...
}
catch (Exception ex)
{
...
}
}
}
#2
2
If you know what the offending value is, can you not just write a unit test for that method and debug it that way?
如果您知道有什么问题,那么您是否可以为该方法编写单元测试并以这种方式进行调试?
If not, if you know the exception, you could set up your debugger to break when that exception is thrown. Go to Debug | Exceptions and check Thrown for the exception in question.
如果没有,如果您知道异常,则可以将调试器设置为在抛出异常时中断。转到Debug |例外和检查抛出有问题的例外。
#3
0
I wonder if you are getting a stack overflow. Does VS track all the values for str or anything to do with each state of Bar? If so, the thousands of copies might add up.
我想知道你是否有堆栈溢出。 VS是否跟踪str的所有值或任何与Bar的每个状态有关的值?如果是这样,可能会增加数千份副本。
I wonder if you could eliminate the problem monitoring the value via a global variable instead, rather than one within the function.
我想知道你是否可以通过全局变量来消除监视值的问题,而不是函数中的一个。