PDA

View Full Version : Crash gracefully?



ultim8
3rd July 2010, 18:50
Hello,

I'm looking for a (cross platform) way of catching various crashes (double free, referencing invalid memory, division by zero, etc...) so that I can log them.

Currently I have my own message handler installed to catch standard Qt messages (QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg). This handler pushes the messages to stderr, but also logs them to a log file.

My hope is I can also catch non Qt errors and send them through the same message handler. The thing is I'm not sure of the correct way to do this (if there even is a correct way).

squidge
3rd July 2010, 19:48
On Windows you need to install an exception frame.

I don't know about Linux.

SixDegrees
3rd July 2010, 19:48
Invalid memory exceptions are not defined by the C++ specification; they are provided by some compilers and environments, but this is platform dependent. The best way to deal with them is through thorough testing that eliminates such access attempts, ensuring that they will never occur during runtime

The same is true for division by zero - there aren't any defined runtime exceptions thrown - but this is very easy to check yourself at runtime simpy by checking the divisor.

Zlatomir
3rd July 2010, 20:10
Well, nice question (i think)

But the answer isn't so nice (simple)
The proper usage of OOP principles is the portable "way" of preventing most of the things that you enumerated, so encapsulate dynamic allocated memory in class, allocate in constructor, delete in destructor)

Or maybe <if necessary> use some smart pointers (http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/smart_ptr.htm) solve some of that problems
PS: the link is to boost libraries (just for exemplification of concept, Qt must have at least some of them)

LE: i think that you must concentrate on not to crash (or very rare crash) not on "beautiful crashes", so coding the right way usually saves you from that.

agathiyaa
3rd July 2010, 20:49
Hi,

Signal handlers may help....~! and it may work in both OS(Linux & Windows)


http://en.wikipedia.org/wiki/Signal_(computing)

ultim8
6th July 2010, 16:48
All:

Yes, the correct thing to do is make it so your application never crashes via regression testing, unit tests, alpha/beta testing.... and of coarse smart coding. But the truth of the matter is, it can and DOES happen. If and when it does happen, I want a log of it so I can more easily find the issue and fix it.

Agathiyaa:

Yes, I actually ran into Signal Handlers on Wikipedia shortly after posting my original post. I haven't had time to look too deeply into them, but they look promising. They are part of the C standard too, so should be compatible cross platform. I'll post more once I try them out.

squidge
6th July 2010, 19:10
Instead of rolling your own, maybe you could use one of the pre-existing exception handlers and concentrate your efforts and getting your app bug free :)

There are lots of different ones which you just bolt to your app once you have something that works. They log all the info they can, some will try and email you the results, etc.

lni
7th July 2010, 05:00
See this: http://www.qtcentre.org/threads/32243-Crash-gracefully-No-crash!