Catch Segfault or any other errors/exceptions/signals in C++ like catching exceptions in Java

前端 未结 5 1486
长情又很酷
长情又很酷 2020-12-11 03:43

I wrote a Linux program based on a buggy open source library. This library sometimes triggers segfaults that I cannot control. And of course once the library has segfaults,

5条回答
  •  独厮守ぢ
    2020-12-11 04:12

    If you have access to the source, it might be useful to run the programmer in a debugger like GDB. GDB stops at the line which causes the segfault.

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

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

提交回复
热议问题