简体   繁体   中英

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.

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).

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). 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 below steps

1- Go to Visual Studio IDE

2- Select Debug options

3- Click on Exceptions

4- Check Throw option for following 'Common Language RunTime Exception' and Native Win 32 Exceptions

5- Run your code in DEBUG mode first.

6- Check you are getting exceptions or not.

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". 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.

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; 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. See my answer here for the basic steps required - and check you are generating PDB symbols for Release builds too.

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.

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