catching unexpected segmentation fault and performing cleanup

G

g35rider

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
 
I

Ian Collins

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.
 
M

Mark

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
 
G

g35rider

Mark said:
It is possible to catch SIGSEGV/SIGBUS on Solaris.

Sorry I forgot to specify I am on Linux RH9 and FC5

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?
 
M

mlimber

Sorry I forgot to specify I am on Linux RH9 and FC5

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
 
N

Noah Roberts

Mark wrote:

Sorry I forgot to specify I am on Linux RH9 and FC5

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.
 
F

Frank Puck

mlimber said:
(e-mail address 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top