Skip to main content

IAR Embedded Workbench for RL78 5.20

CATCH-xtor-bad-member (C++ only)

In this section:
Synopsis

Exception handler in constructor or destructor accesses non-static member variable that might not exist.

Enabled by default

No

Severity/Certainty

Medium/Low

mediumlow.png
Full description

The exception handler in a constructor or destructor accesses a non-static member function. Such members might or might not exist at this point in construction/destruction and accessing them might result in undefined behavior. This check is identical to MISRAC++2008-15-3-3, MISRAC++2023-18.3.3.

Coding standards
MISRA C++ 2008 15-3-3

(Required) Handlers of a function-try-block implementation of a class constructor or destructor shall not reference non-static members from this class or its bases.

MISRA C++ 2023 18.3.3

(Required) Handlers for a function-try-block of a constructor or destructor shall not refer to non-static members from their class or its bases

Code examples

The following code example fails the check and will give a warning:


int throws();

class C
{
public:
  int x;
  static char c;
  C ( )
  {
    x = 0;
  }

  ~C ( )
  {
    try
    {
      throws();
      // Action that may raise an exception
    }
    catch ( ... )
    {
      if ( 0 == x ) // Non-compliant – x may not exist at this point
      {
        // Action dependent on value of x
      }
    }
  }
};

The following code example passes the check and will not give a warning about this issue:

class C
{
public:
  int x;
  static char c;
  C ( )
  {
    try
    {
      // Action that may raise an exception
    }
    catch ( ... )
    {
      if ( 0 == c )
      {
        // Action dependent on value of c
      }
    }
  }

  ~C ( )
  {
    try
    {
      // Action that may raise an exception
    }
    catch (int i) {}
    catch ( ... )
    {
      if ( 0 == c )
      {
        // Action dependent on value of c
      }
    }
  }
};