Warning C6242

A jump out of this try-block forces local unwind. Incurs severe performance penalty

This warning indicates that a jump statement causes control-flow to leave the protected block of a try-finally other than by fall-through.

Remarks

Leaving the protected block of a try-finally other than by falling through from the last statement requires local unwind to occur. Local unwind typically requires approximately 1000 machine instructions, so it's detrimental to performance.

Use __leave to exit the protected block of a try-finally.

Code analysis name: LOCALUNWINDFORCED

Example

The following code generates this warning:

#include <malloc.h>
void DoSomething(char *p); // function can throw exception

int f( )
{
   char *ptr = 0;
   __try
   {
      ptr = (char*) malloc(10);
      if ( !ptr )
      {
         return 0;   //Warning: 6242
      }
      DoSomething( ptr );
   }
   __finally
   {
      free( ptr );
   }
   return 1;
}

To correct this warning, use __leave as shown in the following code:

#include <malloc.h>
void DoSomething(char *p);
int f()
{
   char *ptr = 0;
   int retVal = 0;

   __try
   {
      ptr = (char *) malloc(10);
      if ( !ptr )
      {
         retVal = 0;
         __leave;   //No warning
      }
      DoSomething( ptr );
      retVal = 1;
   }
   __finally
   {
      free( ptr );
   }

   return retVal;
}

The use of malloc and free have many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, use the mechanisms that are provided by the C++ Standard Library. These include shared_ptr, unique_ptr, and vector. For more information, see Smart pointers and C++ Standard Library.

See also

try-except statement
try-finally statement