Missing overload of allocation or deallocation function
Only one function in an allocation-deallocation function pair is overloaded
Description
This defect
occurs when you overload operator new but do not overload the corresponding
operator delete, or vice versa.
Risk
You typically overload operator new to perform some bookkeeping in
addition to allocating memory on the free store. Unless you overload the corresponding
operator delete, it is likely that you omitted some corresponding
bookkeeping when deallocating the memory.
The defect can also indicate a coding error. For instance, you overloaded the placement
form of operator
new[]:
void *operator new[](std::size_t count, void *ptr);
operator
delete[]:void operator delete[](void *ptr);
void operator delete[](void *ptr, void *p );
Fix
When overloading operator new, make sure that you overload the
corresponding operator delete in the same scope, and vice versa.
For instance, in a class, if you overload the placement form of operator
new:
class MyClass {
void* operator new ( std::size_t count, void* ptr ){
...
}
};
operator
delete:class MyClass {
void operator delete ( void* ptr, void* place ){
...
}
};To find the operator delete corresponding to an operator
new, see the reference pages for operator
new and operator delete.
Examples
Result Information
| Group: Good practice |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
MISSING_OVERLOAD_NEW_DELETE_PAIR |
| Impact: Low |
Version History
Introduced in R2019a
See Also
Invalid free of pointer | Invalid deletion of pointer | Memory leak | Mismatched alloc/dealloc
functions on Windows | Find defects (-checkers)
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)