0

because I often have to use libraries from third party - and sometime they will cause my apps to crash - I have made a test library with C++. This library generates a stack overflow (by recursively calls a method) with intent. The test app, made with .NET 4.6, calls the exported method... and crashes.

  • Try-Catch? Will not be reached and has no effect.
  • AppDomain.UnhandledException? Will not be reached and has no effect.
  • Execute the method in an own AppDomain? Has no effect.

Catching a "normal" exception raised by throw is no problem for my test app.

How can I catch this kind of exceptions?

Deep-Sea
  • 1
  • 1
  • 2
    Possible duplicate of [Can you catch a native exception in C# code?](http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code) – Mighty Badaboom May 03 '17 at 12:46
  • No, this here is an other problem. Catching a "normal" exception raised by "throw" is no problem for my test app. – Deep-Sea May 03 '17 at 12:51
  • 2
    Maybe you should read this: http://stackoverflow.com/questions/40144571/is-it-possible-to-catch-a-segfault-with-try-catch – Simon Kraemer May 03 '17 at 12:58
  • Stack overflows in native code are more severe than typical native exceptions, so I wouldn't call it a duplicate. The problem here is that the OS raises the stack overflow exception when it can't grow the stack, but how does this exception return to the caller? You've got a bad stack at that point, so a stack unwind is troublesome. Also, a native (Win32) exception will not run C++ destructors on that corrupted stack. – MSalters May 03 '17 at 13:00
  • 2
    If you could catch it then this site would not have the name it has. – Hans Passant May 03 '17 at 13:20
  • 1
    http://stackoverflow.com/a/13567016/17034 – Hans Passant May 03 '17 at 13:26
  • Thanks @HansPassant for this link - if this is correct, it's bad for our software stability. – Deep-Sea May 08 '17 at 08:35
  • 1
    Not exactly, it doesn't make your software instable. It would have to be instable and inadequately tested first to trigger any of these gross mishaps. It is very rare that it ever gets that far, if such a bug does escape from QA then use a tool like DebugDiag to troubleshoot it in production. – Hans Passant May 08 '17 at 08:48

1 Answers1

0

From a C# perspective, you don't catch the exception. Stack overflows in native code are pretty bad. The calling thread may have half an object on the stack, and there's no way you can unwind that.

Realistically speaking, your options are to suspend the offending thread, which means the C++ code won't return to C#. This isn't directly fatal - threads are suspended all the time by the OS, when no CPU core is available. But mutexes will remain locked etc.

While the offending thread is suspended, your C# code should perform an emergency save of all things that can be rescued. Next, schedule a restart (the Windows Task Scheduler can help here), and forcefully terminate the whole process.

This leaves out one "small" detail: catching the stack overflow and suspending that thread. This is probably best done with a Vectored Exception Handler, which is called directly by Windows. In this exception handler, check the exception object to see if it's a stack overflow. If so, you have a problem, since you're now running on a thread that is down to its last stack page (and that too might be almost full). So, you only do two things: call ResumeThread(helperThread) and then SuspendThread(GetCurrentThread()). The helper thread wakes up, calls back to C# to initiate the emergency save, and then calls TerminateProcess.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • An other thread don't helps - the app will also crash the same way. So I can't catch/detect the error. And as a result of that, I can't suspend a thread or do anything else. – Deep-Sea May 08 '17 at 08:38
  • @Deep-Sea: The app will indeed crash, that's unavoidable. But " crashing" isn't magic. It may be Undefined Behavior in C++, but it's well-defined in Win32. The Vectored Exception Handler manages the crash. – MSalters May 08 '17 at 08:49