Howto prevent process crash on CRT error C++

2.3k Views Asked by At

I have a 3rd party C library which can crashes on CRT error. In this case the whole process crashes. Is there any way to catch the all the CRT errors and prevent process termination.

3

There are 3 best solutions below

3
On

Preventing the crash is probably just delaying the problem.

Imagine that you could prevent the crash, but as a result, the user will save incorrect data back to the database, or corrupts data files, then you are just making the problem worse.

Instead, try to find out what the exact problem is:

  • are you passing incorrect arguments to a function in the 3rd party library?
  • or is it a bug in the 3rd party library? Confront the library manufacturer with the bug.

Alternatively:

  • try to find a work around to the problem
  • find an alternative for the library

EDIT: To be honest, I encountered the same situation last year with a 3rd party component. What I did was the following:

First, use a _try/_except construction to catch the problem. This only works if you known in which function call it exactly crashes. It works like this:

__try
   {
   Some3rdPartyLibraryFunction();
   }
__except (EXCEPTION_EXECUTE_HANDLER)
   {
   }

Second, to prevent further corruption of your application, make sure that the 3rd party library is not called anymore within your application. E.g. suppose that the library is a reporting component, then if you encounter the crash, don't allow the user to open a report anymore, like this:

bool MyClass::openReport (char *reportname)
   {
   if (!reportModuleEnabled)
      return false;

   __try
      {
      OpenTheReport(reportname);
      }
   __except (EXCEPTION_EXECUTE_HANDLER)
      {
      // Tell the user about the problem and prevent further access to the library
      ShowMessage ("Sorry, no more reports");
      reportModuleEnabled = false;
      return false;
      }
   return true;
   }
1
On

Your program runs on Windows? Have you tried wrapped the offending code with SEH? http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657(v=vs.85).aspx

0
On

What a pity, nobody get the point. The CRT error is nothing about SEH, so it can't be catch by _try..._except. You should use the _set_invalid_parameter_handler and _set_purecall_handler functions handle the CRT error.

http://crashrpt.sourceforge.net/docs/html/exception_handling.html