C ++: is it safe to use longjmp and setjmp?

Is it possible to use longjmp and setjmp in c ++ in linux / gcc with respect to the following?

  • Exception handling (I do not do exception handling with longjmp / setjmp. I want to know what side effects longjmp / setjmp will have for standard exception handling)
  • *this pointer
  • Signals
  • Smart pointers (enhancing generic and intrusive pointers)
  • Everything you can think of.
+32
c ++ gcc linux exception longjmp
03 Sep '09 at 21:32
source share
3 answers

setjmp() / longjmp() completely undermine as well as exception handling, as well as RAII (destructors in general).

From 18.7 / 4 "Other runtime support" in the standard:

If any automatic objects are destroyed by passing the excluded exception control to another (target) point in the program, then calling longjmp(jbuf, val) at the throw point, which transfers control to the same (target) point, has undefined behavior.

So the bottom line is that setjmp() / longjmp() doesn't play very well in C ++.

+54
Sep 03 '09 at 21:36
source share

This does not apply to Linux or gcc; setjmp / longjmp and C ++ do not work too well together if you use longjmp to leave a context where there are automatic variables with destructors.

Destructors will not start, which can lead to a memory leak or other bad behavior.

+5
Sep 04 '09 at 21:51
source share

I only found out about these commands and never saw them in action in real applications.

IMHO, it is safe to say that it is unsafe to use them: developers will not understand what these "unpopular" APIs are doing.

-8
Sep 03 '09 at 21:52
source share



All Articles