简体   繁体   English

捕获仅在发布时发生的.NET错误,不会引发任何异常

[英]Catch a .NET error that only happens on Release, no exception thrown

I currently am witnessing an error that only happens on "Release" mode of my exe. 我目前正在目睹仅在我的exe的“发布”模式下发生的错误。

Therefore, I have no debugger attached, and the application only goes "... has stopped working.". 因此,我没有连接调试器,应用程序仅显示“ ...已停止工作”。

My initial reflex was to catch any and all exception in my main loop and display its message, but turns out none is thrown, the program just crashes. 我最初的反应是捕获主循环中的所有异常并显示其消息,但结果没有抛出异常,程序只是崩溃了。 (My program is single threaded). (我的程序是单线程的)。

This might be related to the fact that I integrate unmanaged code, but even then, why would it be different on release mode? 这可能与我集成了非托管代码的事实有关,但是即使那样,为什么发布模式也会有所不同? Is there any way for me to catch that exception? 我有什么办法可以捕捉到该异常?

I suspect it to be one of those errors that go "Cannot show stack trace/find code" when run in the debugger, (and don't actually throw an exception), but I honestly can't test it. 我怀疑它是在调试器中运行时出现“无法显示堆栈跟踪/查找代码”的错误之一(并且实际上并未引发异常),但是老实说我无法对其进行测试。 Suggestions SO? 建议呢?

You can still attach a debugger to it even if it's running in Release mode. 即使调试器在发布模式下运行,您仍然可以将其附加到调试器。 You could try something like... 您可以尝试类似...

  • Write the program so that it waits for a keypress at the beginning of execution 编写程序,使其在执行开始时等待按键
  • Run it in Release mode 在发布模式下运行
  • Attach the debugger while it is waiting for the keypress 在等待按键时附加调试器
  • Debug it 调试一下

Then see what happens. 然后看看会发生什么。 If it stops happening and works under the debugger even when running in Release mode, then you have a Heisenbug (basically meaning it will be very difficult to find this bug). 如果它停止发生并且即使在发布模式下运行也可以在调试器下工作,则您有一个Heisenbug (基本上意味着很难找到此错误)。

If, however, it happens and the Visual Studio debugger breaks when the problem happens, then look at the Threads window (Ctrl+Alt+H, I think). 但是,如果发生这种情况,并且问题发生时Visual Studio调试器中断,请查看“线程”窗口(我认为是Ctrl + Alt + H)。 It is possible that, although your application uses only one thread, the native code you ran could start unmanaged threads of its own. 尽管您的应用程序仅使用一个线程,但是您运行的本机代码可能会启动自己的非托管线程。 If that is the case, you might be able to find a way to get it to stop doing that, as unfortunately there is no way to catch that exception in your managed code. 如果真是这样,您可能能够找到一种方法来停止这样做,因为不幸的是,您无法在托管代码中捕获该异常。

That may be due to the fact you are using try catch block some where 那可能是由于您在某些地方使用try catch块

Try below steps 请尝试以下步骤

1- Go to Visual Studio IDE 1-转到Visual Studio IDE

2- Select Debug options 2-选择调试选项

3- Click on Exceptions 3-点击例外

4- Check Throw option for following 'Common Language RunTime Exception' and Native Win 32 Exceptions 4-检查抛出选项,以遵循“公共语言运行时异常”和本机Win 32异常

5- Run your code in DEBUG mode first. 5-首先在调试模式下运行代码。

6- Check you are getting exceptions or not. 6-检查您是否有例外。

this may solve your problem atleast you will get the exception in DEBUG mode. 这可能至少解决了您的问题,您将在调试模式下获得异常。

Two cents here: 两分钱在这里:

I ran into an issue in a winforms application recently where I would receive the exception "object reference not set to an instance of an object". 最近,我在winforms应用程序中遇到一个问题,我会收到异常“对象引用未设置为对象实例”。 This only occurred in release mode and not debug mode. 这仅在发布模式下发生,而不在调试模式下发生。

I was able to run the program as release with limited debugging and could see all the variables. 我能够在有限的调试下作为发行版运行程序,并且可以看到所有变量。 None of them had any issues. 他们都没有任何问题。

Anyway, I literally just upgraded visual studio from version 15.7.1 to 15.7.3 and the problem went away. 无论如何,我实际上只是将Visual Studio从15.7.1版本升级到15.7.3,问题就消失了。

The layout of memory will be different between Release and Debug. 发行版和调试版之间的内存布局将有所不同。 Also the layout of the stack may be different. 堆栈的布局也可能不同。 If you have a buggy piece of unmanaged code trashing memory, then it's going to have random effects. 如果您有一段无法管理的代码浪费内存,那么它将产生随机影响。

You can still step debug a Release build from VS; 您仍然可以从VS逐步调试Release版本。 try that first. 请先尝试。

If the mixing of managed and unmanaged code makes it tricky for some reason, you could try using WinDbg with the SOS and SOSEX extensions. 如果托管代码和非托管代码的混合由于某种原因而变得棘手,则可以尝试将WinDbg与SOS和SOSEX扩展一起使用。 See my answer here for the basic steps required - and check you are generating PDB symbols for Release builds too. 请参阅我的回答了解所需的基本步骤-并检查您是否也在为Release版本生成PDB符号。

I think it's better to test the unmanaged code in isolation first. 我认为最好先隔离测试非托管代码。

If this is successful, then the problem maybe the integration of the unmanaged code to managed code or in the managed code itself. 如果成功,则可能是非托管代码与托管代码或托管代码本身集成的问题。

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

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