program crashes

This is a discussion on program crashes within the Linux Programming forums, part of the Platform Specific Boards category; Okay, I'm writing a program , and it tends to crash often. So I was curious, how would I detect ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    program crashes

    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.
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    signal handlers

    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.
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 12:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21