468,278 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

catching unexpected segmentation fault and performing cleanup

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
6 4791
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
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

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

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

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

Similar topics

15 posts views Thread by Dennis Schulz | last post: by
5 posts views Thread by Fra-it | last post: by
27 posts views Thread by Paminu | last post: by
7 posts views Thread by pycraze | last post: by
3 posts views Thread by madunix | last post: by
6 posts views Thread by DanielJohnson | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
1 post views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.