2

I am currently managing a complicated application. It's written in C# and .Net 4.7.2.

Sometimes this program shuts down without notice. No error message even with a try/catch block and MessageBox.Show() in the Main method (I know it's probably not the best way but should work).

There are several threads running at different points, calling external DLLs and sometimes even drivers. So in order to log whether it's another thread that crashes the whole thing, I do this at the beginning :

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;

Because I'm not sure which one is the correct one. In the methods, I log the Exception (after performing null checks) into a file (using File.AppendText and a timestamped based file).

Still nothing. The application keeps crashing after some random amount of time (between 2 and 6 hours) and I have no log information, no error message and I'm getting kind of lost here.

The app is running in Release mode and I cannot use Visual Studio to run the debugger into it (because that would make it easy). Maybe there's another way to run an external debugger ?

Can someone give me a hint on how to catch up for an exception that would cause an application to crash silently ?

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • 4
    What does event log tell you? – Pavel Anikhouski Jun 12 '20 at 13:22
  • 2
    I don't get your statement `The app is running in Release mode and I cannot use Visual Studio to run the debugger` why can't you attach to your running process? - https://learn.microsoft.com/en-us/visualstudio/debugger/attach-to-running-processes-with-the-visual-studio-debugger but as @PavelAnikhouski already said when your application crashes it should always produce an entry in the windows event viewer - https://www.howtogeek.com/123646/htg-explains-what-the-windows-event-viewer-is-and-how-you-can-use-it/ – Rand Random Jun 12 '20 at 13:41
  • @PavelAnikhouski Thanks for you comment, I will check next time I'm on site – Martin Verjans Jun 15 '20 at 07:05
  • `ExecutionEngineException` or [`Environment.FailFast`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.failfast?view=netcore-3.1) method can't by caught be the application code, it's written only to event log – Pavel Anikhouski Jun 15 '20 at 07:12
  • 2
    @PavelAnikhouski Using Windows Event Logs I was able to find out this was caused by a MemoryAccesViolationException, due to an array copy operation. The array was bigger than the struct. Feel free to post this as an answer as it answers my question. Thanks again. – Martin Verjans Jun 19 '20 at 08:06
  • @MartinVerjans Glad, that my hint helps you:) I don't think, that I've answered your question, you investigated everything by yourself – Pavel Anikhouski Jun 19 '20 at 08:12

2 Answers2

1

Based on your explanations the only thing that brings to my mind is that you have some fire and forget threads in your application that throw exception sometimes but your application can't keep track of them to log or catch their exceptions.

Make sure all your tasks are awaited correctly and you don't have any async void method.

If you really need some fire and forget actions in your app, at least keep them alive with something like private Task fireAndForgetTaskAliver in your classes.

Another probability could be memory leak in your app that causes stack overflow exception.

Arman Ebrahimpour
  • 4,252
  • 1
  • 14
  • 46
  • 1
    Or `OutOfMemoryException` – Rand Random Jun 12 '20 at 13:43
  • 3
    `StackoverflowException` and `OutOfMemoryException` won't be catched anymore this has been changed for quite some time, if you would like to catch them you would need to set legacyCorruptedStateExceptionsPolicy - https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/legacycorruptedstateexceptionspolicy-element - or this https://stackoverflow.com/questions/39956163 – Rand Random Jun 12 '20 at 13:47
  • Thanks for you answer, however I know there are no async operations as this is code that has been written before async tasks. – Martin Verjans Jun 15 '20 at 07:07
1

The only way to catch an exception that is not caught anywhere in the code is indeed to look it the Windows Event Log, under Applications.

Thanks to Pavel Anikhouski for his comment.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48