V1302. The 'new' operator cannot be used outside of a try..catch block in a parallel section
The analyzer found an error relating to throwing an exception from a parallel block. According to OpenMP specification, if you use exceptions inside a parallel block all these exceptions should be processed inside this block. If you use "new" operator inside parallel code you should provide interception of the exception which will be generated when an error of memory allocation occurs according to C++ standard. The example given below leads to incorrect program's behavior and most likely to a program crash if an error of memory allocation occurs:
void foo1302(ptrdiff_t n)
{
#pragma omp parallel for
for (ptrdiff_t i = 0; i < n; i++)
{
float *array = new float[10000];
//...
delete [] array;
}
}
Correction of the code lies in processing exceptions inside the parallel block and transferring the information about the error through other mechanisms or in refusing to use "new" operator. This is the corrected variant of the function:
void foo1302_fixed(ptrdiff_t n)
{
#pragma omp parallel for
for (ptrdiff_t i = 0; i < n; i++)
{
try {
float *array = new float[10000];
//...
delete [] array;
}
catch (std::bad_alloc &) {
// process exception
}
}
}