...
Resources must not be leaked as a result of throwing an exception, including during the construction of an object.
This rule is a subset of MEM51-CPP. Properly deallocate dynamically allocated resources, as all failures to deallocate resources violate that rule.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <new> struct SomeType { void process_item() noexcept(false); }; void f() { SomeType *pst = new (std::nothrow) SomeType(); if (!pst) { // Handle error return; } try { pst->process_item(); } catch (...) { // Handle error delete pst; throw; } delete pst; } |
This compliant solution complies with MEM51-CPP. Properly deallocate dynamically allocated resources. However, although handling resource cleanup in catch
clauses works, it can While this compliant solution properly releases its resources using catch
clauses, this approach can have some disadvantages:
- Each distinct cleanup requires its own
try
andcatch
blocks. - The cleanup operation must not throw any exceptions.
...