Catch Segfault or any other C ++ errors / exceptions / signals, such as catch exceptions in Java

I wrote a Linux program based on the open source crimson library. This library sometimes calls segfaults which I cannot control. And, of course, after segfault happens in the library, the whole program dies. However, I have to make sure that my program continues to work, even if the library has segfaults. This is due to the fact that my kind of program serves as a “server”, and it needs to at least tell clients that something bad has happened and fix the errors, not the chicken ... Is there a way to do this?

I understand that in Java you just need to catch the exception. But how does C ++ handle this?

[UPDATE] I understand that C ++ has exception handling, but Segfault is not an exception, is it? I do not think that something is thrown when segfault happens. You must explicitly throw something to use try .... catch ... as far as I know.

Thanks a lot, I'm pretty new to C ++.

+8
c ++ segmentation-fault exception exception-handling signals
source share
5 answers

You cannot reliably resume execution after a segmentation violation. If your program should remain running, open the library of insults in a separate process and communicate with it through the channel. When a segmentation violation occurs, your program will see a closed channel.

+14
source share

Sorry, you cannot continue the program. The buggy code that led to SIGSEGV usually causes undefined behavior, such as dereferencing a null pointer or reading garbage memory. You will not be able to continue if your code is working with invalid data.

You can process the signal, but most of all you can do is reset the stack trace and die.

C and C ++ are essentially unsafe ; you cannot handle errors caused by undefined behavior and continue executing the program.

+4
source share

You can use signal handlers. This is really not recommended because you cannot guarantee that you have fixed the cause of the problem. It would be best to isolate it in a separate process - this is the approach that Google Chrome uses.

If it's FOSS, the easiest way is to just debug it.

+3
source share

If you have access to the source, it may be useful to run the programmer in a debugger such as GDB. GDB stops at the line that invokes segfault.

If you really want to catch a signal, you need to hook up a signal handler using the signal system call . I would probably just stick with a debugger.

EDIT: Since you write that the segfaults library, I would just like to point out the first programming rule: always your mistake . Especially if you are new to C ++, segfault probably happens because you used the library incorrectly. C ++ is a very subtle language, and it's easy to do what you don't intend to.

+2
source share

As mentioned more here, you cannot catch Segfault signals from TRY blocks or a "map" breaking a segment to anything. It is a really bad idea to deal with SIGSEGV on your own. SEGV from C ++ code is a serious mistake. You can use GDB to find out why and solve it.

0
source share

All Articles