By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

catching unexpected segmentation fault and performing cleanup

P: n/a
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 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
g3******@gmail.com 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.
Aug 15 '06 #2

P: n/a
On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>g3******@gmail.com 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

Aug 15 '06 #3

P: n/a

Mark wrote:
On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <ia******@hotmail.com>
wrote:
g3******@gmail.com 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 '06 #4

P: n/a
g3******@gmail.com wrote:
Mark wrote:
On Tue, 15 Aug 2006 18:17:23 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>g3******@gmail.com 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

Aug 15 '06 #5

P: n/a

g3******@gmail.com 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.

Aug 15 '06 #6

P: n/a

"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
g3******@gmail.com 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.

Aug 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.