Re: Detection of fatal errors
On Jul 24, 12:27 pm, sebastian <Sebastian.P...@googlemail.com> wrote:
Hi,
Is there any way to perform an action before program's exit, when it
encounter some kind of fatal error?
Namely I'd like to detect a fatal error and dump buffer with logs to a
file in situations like below:
---- example 1 ----
int *a = 0;
*a = 21;
---- example 1 ----
MyClass *a;
a->someFunction();
I've tried many things:
(1)
try {
} catch( ... ) {
dumpBuffer();
}
(2)
std::set_unexpected( dumpBuffer );
std::set_terminate( dumpBuffer );
(3)
atexit( dumpBuffer ); // this is bypassed anyhow...
None of them works.
My platform is Windows XP, program is written using Qt 4.4.0, compiler
is gcc (Mingw) 3.4.5.
Cheers,
Sebastian
--
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
There is no C++ standard way to recover from dereferencing a null
pointer. Once that happens, the C++ standard says you're on your own.
There are many kinds of fatal errors. Dereferencing a null pointer is
one of them.
My suggestion is to write better code, put in asserts, use less naked
pointers, and a wide variety of other good C++ styles which will make
such code less likely.
Another option is using static time code analyzers to catch a lot of
these problems. You can also use runtime emulators to run your program
and catch problems like this at run time. Some language extensions,
like how Visual Studios 2003 can throw an exception at null pointer
dereferences, but that is implementation specific and not portable.
However, you will always have this problem, and there is no way to fix
all such problems, either at runtime or compile time.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]