UnhandledExceptionFilter not called with redirected stderr output
I am having a problem hitting an UnhandledExceptionFilter set by SetUnhandledExceptionFilter() when the stderr is being redirected.
There is an custom exception filter set by SetUnhandledExceptionFilter(), that is supposed to write out details about the exception as well as the stack to stderr. The problem is, that it is not working when I redirect the stderr output into a file. It is not even called at all.
If I execute the program using
app.exe > log
the filter is called, but if I execute it like this
app.exe 2> log
it isn't called. I already tried a different approach since I call the application in question from another program using system(). I used _pipe() and _spawnvp() to redirect the output and reading it from the pipe, but that didn't help.
I started looking into AddVectoredExceptionHandler() and it seems to work, but it is called on any exception and I have no idea how to figure out how this plays out with try/catch code since I just want the unhandled exceptions.
I am already aware, that an UnhandledExceptionFilter is not called when the program is run in a debugger (http://support.microsoft.com/kb/173652/en-us), but why does it treat a redirection of stderr like it is run in a开发者_高级运维 debugger? Any ideas how to prevent this?
Found my stupid mistake.
The exception filter is called, but since the application terminates after the function the data is still stuck in the stream and is never flushed. If I add
fflush(stderr);
at the end of my exception filter the stuff is actually written.
Sorry for once again figuring my mistake out short after posting it, but this is actually an issue, that bugged me for months and not just a few hours.
精彩评论