PDA

View Full Version : program crashes



Strut
02-09-2002, 08:17 PM
Okay, I'm writing a program , and it tends to crash often. So I was curious, how would I detect a program crash, and then do something after it. For instance, when it crashes log to a file what function crashed it, time/date, ect...

I don't need to time/date, but a printing out what function crashed it is something I need to know. I looked into atexit() and its not what I'm looking for for. If your curious, I am writing a MUD game, so I work with lots of different players playing at once. So when it crashes I need to save each character so that no data is lost. and then print out what function caused it so I can fix it.

any help would be most appreciated.

starX
02-09-2002, 11:48 PM
I may be wrong about this, but my guess would be you would want to run the actual program as a child of another process that is watching the child's exist status, but that would involve a lot of IPC between the two processes. There might be something worth checking out in errno.h

Then again, you could always try using gdb or something to that effect to find out exactly what your program is doing that makes it crash. If it runs for a while and then crashes, it's probably some kind of memory leak or other such error.

starX
www.axisoftime.com

Salem
02-10-2002, 01:15 AM
> Okay, I'm writing a program , and it tends to crash often
segmentation fault, core dumped?

As starX says, use a debugger - it will stop the program in the function detecting the problem, which hopefully will have enough information to allow you to work out which function caused the problem.

Things to look out for
- uninitialised pointers
- overstepping the bounds of an array (either static arrays, or ones from malloc)
- not freeing memory
- freeing memory twice
- freeing memory which wasn't malloc'ed to start with
- using memory which has been free'd

> So when it crashes I need to save each character so that no data is lost
When you've got the problems fixed, and you have some very rare problem to fix (this should not be normal behaviour), you can write a signal handler for SIGSEGV (and perhaps a few other signals as well, like STOP, TERM), which saves all the data to a 'restore' file. You don't want to save it to the actual file in case the data itself is also corrupt.

Strut
02-10-2002, 10:49 AM
How would I write a signal handler as you said? Or where can I find info?

Would it also allow me to recover from a crash? Being the game is multiplayer, it would be really helpfull to have a crash recovery so every one doesn't get thrown off. Or atleast restore their character stats once a crash has happened, instead of losing valuable information.

Thank you so far, I really appreciate it.

ps. I am using GDB at current, but this deesn't help me save player stats.