Throw argument expression calls
new
The argument expression in a throw
statement allocates memory by
calling new
, which can result in abrupt termination of the program and
resource leaks
Since R2023b
Description
This defect occurs when the argument expression of a throw
statement
allocates memory by calling new
. For instance, consider this
code:
try{ //... throw new std::string{"Error"}; }catch(...){ //... }
throw
expression calls new
to allocate memory before
raising the exception. Polyspace® reports a defect.Risk
A failed call to
new
can result in an exception, such asstd::bad_alloc
. Such an exception in thethrow
expression becomes an unhandled exception, resulting in abrupt program termination and resource leaks. SeeArgument expression of throw statement might raise unexpected exception
.If you call
new
when raising an exception, it is not clear where you can safely deallocate the memory. The allocated memory might be leaked.
Fix
To fix this issue, remove the call to new. For instance, instead of this code:
throw new T;
throw T;
Examples
Result Information
Group: C++ Exception |
Language: C++ |
Default: Off |
Command-Line Syntax:
THROW_EXPRESSION_CALLS_NEW |
Impact: Medium |
Version History
Introduced in R2023b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)