Shutdown exception handling for Win32/C++

2020-04-30 12:42发布

问题:

I have a process that handles exceptions great. It calls:

_set_se_translator(exception_trans_func); 
SetUnhandledExceptionFilter(UnhandledExceptionFilterHandler);
_set_purecall_handler(purecallHandler);
set_terminate(terminateHandler);
set_unexpected(unexpectedHandler);
_set_invalid_parameter_handler(InvalidParameterHandler);
atexit(exitHandler); //ignored during an expected exit
_onexit(onexitHandler); //ignored during an expected exit

Anytime an exception happens, one of the handlers is called which creates a crash dump for me. Life is good.

Except at one customer site. When they shutdown the process, there is an exception that isn't routed through these calls for some reason and they get the error:

The instruction at "0x101ba9df" referenced memory at "0x00000004". The memory could not be "read". Click OK to terminate...."

The memory reference of x000000004 looks like it's probably a null pointer. And looking at that address appears to be a global STL object's destructor (probably in the CRT's initterm call where globals are cleaned up).

Right now I'm kind of stuck though since I can't get a diagnostic dump and call stack and see exactly what is going on. So....

Why isn't the exception being routed through the above handlers, and instead being shown to the user?

Is there any way to hide that dialog (since no harm is being done at that point)?

And is there a way to track down the root error?

Thanks for any ideas.

回答1:

What operating system are they running?

I assume you're setting the error mode using something like

::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);

to make sure that windows isn't jumping in with its own error handling?



回答2:

This sounds like the CRT has put an SEH try/catch block (can't write it properly, Markdown kicks in) around some piece of code, and is catching the exception to display the message, so you never end up calling the unhandled exception code path. You might have to do some CRT hacking to figure out what's happening.



回答3:

It could be that STL code is being executed during the destruction of global variables at program shutdown time and perhaps (depending on the version of STL that you're using) some global variables that it requires have already been destroyed.

I've seen this with VS2008's STL. There are some STL lock objects that are created via a file level static during start up.

Are you using STL in your error handler functions? It could be that one of these is going off late in program shutdown and causing the problem.