Matt S.

Hi,

I'm working on a standard Win32 library, and I'd like to catch my own critical errors and log them, but the CRT consistently displays its own error handler instead. I've searched the help files for methods that control critical error handling, but still there's no change. I thought perhaps "SetErrorMode" or "_CrtSetReportMode" might make a difference:

SetErrorMode(SEM_FAILCRITICALERRORS);

_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);

_control87( 0, _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INVALID);

signal( SIGFPE, fph);

So, if anybody knows of a way around the defaults, please send it on.

Thanks,

Matt S.



Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

einaros

I seem to recall this behavior being changed in newer versions of the CRT. One way to get around it is to use SetUnhandledExceptionFilter, then API hook it to make sure it isn't overwritten by the CRT later. The entire thing is a bit hairy, so I'd say that you are better off detecting the cause of the errors, rather than the errors themselves.





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Matt S.

Thanks einaros. I will give that a try. As far as detecting the cause, well, I'm talking about putting the code in a library that goes out to other people in the company and outside the company. So I'm looking to log the error during run-time and continue execution if possible. This was so easy in DOS



Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

SvenC

Hi Matt,

the problem is that after an exception you cannot easily know if continuing will be safe and successful. If somewhere someone caused an access violation then something in the process heap might already be in an invalid state so that further processing will fail also. Another problems are resources held by the code causing the exception. Code which would have run after the point of the exception might have done some clean up, e.g. releasing sync objects. So you might easily run into deadlocks.

Trying to log the error and restart the process might be a saver approach.

--
SvenC





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

einaros

I was looking for a suggestion at connect.microsoft.com about the CRT error handling, and Dr. Watson intervening all the time, but could only find it cached on Google. Have a look at it, though -- it does contain some good points.




Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Simple Samples

Matt S. wrote:
I've searched the help files
If you search more extensively you will find more answers. The most important keyword(s) to use is exception(s).

The one thing that I have a question about is use of C++ Exceptions. I will create a new thread for my question.

The following are from my notes, so they are complete or incomplete according to my requirements and are more useful for me than for you. This should get you started but please assume there are more to be found. Note that except for C++ standard exceptions, this is all off-topic for this forum. Also note that the first article below describes Vectored Exception Handling, which is different from Structured Exception Handling, and I did not see any other articles describing it. Again, however, most all of this is way off-topic for the VC Language forum.

Under the Hood: New Vectored Exception Handling in Windows XP -- MSDN Magazine, September 2001

Bugslayer (SetUnhandledExceptionFilter)

CodeProject Articles






Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Simple Samples

Matt S. wrote:
Thanks einaros. I will give that a try. As far as detecting the cause, well, I'm talking about putting the code in a library that goes out to other people in the company and outside the company. So I'm looking to log the error during run-time and continue execution if possible. This was so easy in DOS
I need something very similar so email me if you want to; we might be able to help each other.




Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

einaros

Simple Samples wrote:
I need something very similar so email me if you want to; we might be able to help each other.

http://einaros.blogspot.com/2006/09/easily-capturing-application-crash.html





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Simple Samples

einaros wrote:
http://einaros.blogspot.com/2006/09/easily-capturing-application-crash.html

Thank you, Einar, but note that my comment replied to something saying that an important requirement is to be able to continue after failure. I totally understand that it is usually dangerous to do that, but my requirements make it more likely that we can continue in spite of failure. Since this isn't my thread, I won't go into more details here. Your article and samples I am sure are useful and I hope I can use what you have when I need something that does what it does.

Or did I miss something






Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

einaros

Simple Samples wrote:

Or did I miss something



That application installs an unhandled exception filter, and hooks the api to install new ones (SetUnhandledExceptionFilter). Unless this hook is done, the new CRT will override any custom filter upon an error occurring.

When an error occurs, the filter function will be run. You can handle the error, log it and continue execution if you so choose. I'm not a big fan of keeping the ship afloat even when broken in two, but this at least allows you to display a sensible error message (and save all current data) before terminating.





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Matt S.

It looks like working with the extensive coverage that SetUnhandledExceptionFilter provides is the only way remaining to handle standard C critical errors. Unfortunately, it also adds quite a bit of standard C++ exceptions to the list. It's true there's no way and possibly no need to continue when something like an array bounds is exceeded or there's an access exception. The possibility that Watson is invoked per Einaros referred link puts the solution back into the "useless" category -- ie, the developer still can't log exceptions and move on. So, I'll have to give this a try.

Thanks to all for their suggestions and feedback. Once I get done with current tasks, I'll give this a try and see how far I will get...

Regards,

Matt





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Matt S.

ALL,

I've finally gotten back to this effort, and working with SetUnhandledExceptionFilter wasn't quite adequate.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/debug/base/unhandledexceptionfilter.htm

Also, as mentioned on the help page referenced above, it's important to call

Code Snippet

SetErrorMode(SEM_NOGPFAULTERRORBOX);

This gets you called for the function you entered into signal().

Also, what's critical is that at the end of your handler, you have to add a call to re-install it! Another call to signal() will do it.

signal( SIGFPE, fph);

Thanks for all the help, and I hope that this is instructive to others.

Regards,

Matt S.





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Simple Samples

What works for me probably won't help you much but for me the greatest vulnerabilities are bad addresses from outside my program. So I am using IsBadReadPtr and IsBadStringPtr to catch the problem before encountering exceptions.






Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

SvenC

Hi,

you might want to rethink using those APIs. See these blog entries for some explanation why IsBadXXXPtr might cause more trouble than help:

http://blogs.msdn.com/larryosterman/archive/2004/05/18/134471.aspx

http://blogs.msdn.com/oldnewthing/archive/2006/09/27/773741.aspx

--

SvenC





Re: Visual C++ Language Need Info -- Can standard C Critical Error Handler still be installed?

Simple Samples

As I indicated, what works for me probably won't help others but for me I don't see a problem.

Traditionally software engineers considered it bad programming to allow software to fail without a useful message and without recovery capability. It is very disapointing that (according to those blogs) Microsoft has decided that instead of trying to provide the ability to issue messages and recover, they will use the easy solution of a hard crash. They therefore won't provide the tools for Windows applications to issue messages and recover. That is so unusual that it is weird.