Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > catching unexpected segmentation fault and performing cleanup

Reply
Thread Tools

catching unexpected segmentation fault and performing cleanup

 
 
g35rider@gmail.com
Guest
Posts: n/a
 
      08-15-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      08-15-2006
On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <(E-Mail Removed)>
wrote:

>(E-Mail Removed) 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

 
Reply With Quote
 
g35rider@gmail.com
Guest
Posts: n/a
 
      08-15-2006

Mark wrote:
> On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <(E-Mail Removed)>
> wrote:
>
> >(E-Mail Removed) 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


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      08-15-2006
(E-Mail Removed) wrote:
> Mark wrote:
> > On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <(E-Mail Removed)>
> > wrote:
> >
> > >(E-Mail Removed) 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-lit....html#faq-5.9).

Cheers! --MM

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      08-15-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
Frank Puck
Guest
Posts: n/a
 
      08-16-2006

"mlimber" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is my code giving a duplicate loops and segmentation fault.. uche C++ 4 03-02-2007 12:26 AM
Segmentation Fault using MySQLdb and twisted jgarber Python 0 08-16-2006 06:20 PM
File reading and writing problem (SEGMENTATION FAULT) utab C++ 7 03-14-2006 02:52 PM
catching segmentation faults from Ruby Iain Dooley Ruby 12 08-29-2005 01:09 PM
POSIX Threading and Segmentation Fault... DanielEKFA C++ 16 05-16-2005 10:27 AM



Advertisments