IBM Support

Issues with mixing setjmp()/longjmp() with C++ exceptions

Troubleshooting


Problem

C++ exception handling does not behave as intended when mixing it with setjmp()/longjmp() functions.

Symptom

Programs that depend on C++ exceptions being automatically cleaned up may be affected by mixing setjmp()/longjmp() functions with C++ exceptions.

Consider the problematic test case below:


$cat test.cpp
#include <iostream>
#include <setjmp.h>

using namespace std;

main()
{
  jmp_buf state;
  int value = 0;
  int count = 0;

  value=setjmp(state);

  printf ("val is %d\n",value);
 
  ++count;

  try
  {
  if (count == 1)
  throw 1;
  }
  catch (...)
  {
    printf ("In catch before longjmp\n");
    longjmp(state, 1);
    printf ("In catch after longjmp\n");
  }

  printf ("After try catch\n");
 
  return 0;
}
$

[{"Product":{"code":"SSGH3R","label":"XL C\/C++ for AIX"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Component":"Compiler","Platform":[{"code":"PF002","label":"AIX"}],"Version":"11.1;12.1;13.1;13.1.2;13.1.3","Edition":"","Line of Business":{"code":"LOB73","label":"Power TPS"}}]

Log InLog in to view more of this document

This document has the abstract of a technical article that is available to authorized users once you have logged on. Please use Log in button above to access the full document. After log in, if you do not have the right authorization for this document, there will be instructions on what to do next.

Document Information

Modified date:
06 December 2018

UID

swg21590839