Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 437
Lazlo Avatar asked Sep 13 '10 02:09

Lazlo


People also ask

What happens if you don't catch an exception C#?

In C#, the catch keyword is used to define an exception handler. If no exception handler for a given exception is present, the program stops executing with an error message. Don't catch an exception unless you can handle it and leave the application in a known state.

What happens when a raised exception is not caught by catch block?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

How do I catch exceptions in Visual Studio?

With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions. If you need to know how to add handlers for exceptions, see Fix bugs by writing better C# code.

Can we use try without catch in C#?

You can also use finally block only with a try block means without a catch block but in this situation, no exceptions are handled. The finally block will be executed after the try and catch blocks, but before control transfers back to its origin.


2 Answers

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.

like image 88
Timwi Avatar answered Sep 24 '22 22:09

Timwi


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.

like image 28
John Saunders Avatar answered Sep 23 '22 22:09

John Saunders