catching unexpected segmentation fault and performing cleanup

Discussion in 'C++' started by g35rider@gmail.com, Aug 15, 2006.

  1. Guest

    Hi, sometimes due to unchecked code I get segmentation faults once in a
    while and would like to be able to catch them and do some cleanup on
    things. This segmentation fault could be anywhere in the code.

    Could it be caught like a signal and perform cleanup then? Or would I
    have to put every piece of function code in try and catch blocks?

    Thanks
    Ankur
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Hi, sometimes due to unchecked code I get segmentation faults once in a
    > while and would like to be able to catch them and do some cleanup on
    > things. This segmentation fault could be anywhere in the code.
    >
    > Could it be caught like a signal and perform cleanup then? Or would I
    > have to put every piece of function code in try and catch blocks?
    >

    On a typical (that is all those I'm aware of!) environment, you can't
    catch a segmentation/memory access fault except while running under a
    debugger or emulator.

    Fix your code.

    --
    Ian Collins.
     
    Ian Collins, Aug 15, 2006
    #2
    1. Advertising

  3. Mark Guest

    On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <>
    wrote:

    > wrote:
    >> Hi, sometimes due to unchecked code I get segmentation faults once in a
    >> while and would like to be able to catch them and do some cleanup on
    >> things. This segmentation fault could be anywhere in the code.
    >>
    >> Could it be caught like a signal and perform cleanup then? Or would I
    >> have to put every piece of function code in try and catch blocks?
    >>

    >On a typical (that is all those I'm aware of!) environment, you can't
    >catch a segmentation/memory access fault except while running under a
    >debugger or emulator.


    It is possible to catch SIGSEGV/SIGBUS on Solaris.

    >Fix your code.


    Agreed.

    Mark
     
    Mark, Aug 15, 2006
    #3
  4. Guest

    Mark wrote:
    > On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <>
    > wrote:
    >
    > > wrote:
    > >> Hi, sometimes due to unchecked code I get segmentation faults once in a
    > >> while and would like to be able to catch them and do some cleanup on
    > >> things. This segmentation fault could be anywhere in the code.
    > >>
    > >> Could it be caught like a signal and perform cleanup then? Or would I
    > >> have to put every piece of function code in try and catch blocks?
    > >>

    > >On a typical (that is all those I'm aware of!) environment, you can't
    > >catch a segmentation/memory access fault except while running under a
    > >debugger or emulator.

    >
    > It is possible to catch SIGSEGV/SIGBUS on Solaris.


    Sorry I forgot to specify I am on Linux RH9 and FC5
    > >Fix your code.

    >
    > Agreed.


    What if it is caused by someone else's program? or library that you
    have no control over, or if you are doing a client server program with
    defined rules and the client breaks the rules?

    > Mark
     
    , Aug 15, 2006
    #4
  5. mlimber Guest

    wrote:
    > Mark wrote:
    > > On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <>
    > > wrote:
    > >
    > > > wrote:
    > > >> Hi, sometimes due to unchecked code I get segmentation faults once in a
    > > >> while and would like to be able to catch them and do some cleanup on
    > > >> things. This segmentation fault could be anywhere in the code.
    > > >>
    > > >> Could it be caught like a signal and perform cleanup then? Or would I
    > > >> have to put every piece of function code in try and catch blocks?
    > > >>
    > > >On a typical (that is all those I'm aware of!) environment, you can't
    > > >catch a segmentation/memory access fault except while running under a
    > > >debugger or emulator.

    > >
    > > It is possible to catch SIGSEGV/SIGBUS on Solaris.

    >
    > Sorry I forgot to specify I am on Linux RH9 and FC5
    > > >Fix your code.

    > >
    > > Agreed.

    >
    > What if it is caused by someone else's program? or library that you
    > have no control over, or if you are doing a client server program with
    > defined rules and the client breaks the rules?


    On some platforms you can do it (e.g., use signal handlers on most UNIX
    platforms or __try/__except/__finally with VC++), but there is no
    standard way. Ask in a newsgroup for your platform or compiler (cf. the
    list at
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9).

    Cheers! --MM
     
    mlimber, Aug 15, 2006
    #5
  6. Noah Roberts Guest

    wrote:
    > Mark wrote:


    > > It is possible to catch SIGSEGV/SIGBUS on Solaris.

    >
    > Sorry I forgot to specify I am on Linux RH9 and FC5
    > > >Fix your code.

    > >
    > > Agreed.

    >
    > What if it is caused by someone else's program? or library that you
    > have no control over, or if you are doing a client server program with
    > defined rules and the client breaks the rules?


    The first should never happen.

    The second you can't do anything about.

    The last, your server program should never trust client code...plain
    and simple. Don't access memory based on input from an external,
    untrusted source.
     
    Noah Roberts, Aug 15, 2006
    #6
  7. Frank Puck Guest

    "mlimber" <> wrote in message
    news:...
    > wrote:
    > On some platforms you can do it (e.g., use signal handlers on most UNIX
    > platforms or __try/__except/__finally with VC++), but there is no
    > standard way. Ask in a newsgroup for your platform or compiler (cf. the
    > list at



    in Visual C++ you can catch invalid memory access using a normal try-catch.
    But you need to compile your code using the /EHa option.
    There is also a callback which you can set to throw your own type of
    exception.
    This callback must be set using _set_se_translator.
    Otherwise catch(...) must be used.
     
    Frank Puck, Aug 16, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DanielEKFA
    Replies:
    16
    Views:
    8,197
    DanielEKFA
    May 16, 2005
  2. jgarber
    Replies:
    0
    Views:
    726
    jgarber
    Aug 16, 2006
  3. utab
    Replies:
    7
    Views:
    3,379
    Dietmar Kuehl
    Mar 14, 2006
  4. uche
    Replies:
    4
    Views:
    338
    David Harmon
    Mar 2, 2007
  5. Iain Dooley
    Replies:
    12
    Views:
    245
    Iain Dooley
    Aug 29, 2005
Loading...

Share This Page