Deallocation of previously deallocated pointer
Memory freed more than once without allocation
Description
This defect occurs when a block of memory is freed more than
once using the free
function without an intermediate
allocation.
Risk
When a pointer is allocated dynamic memory with malloc
,
calloc
or realloc
, it points to a memory
location on the heap. When you use the free
function on this
pointer, the associated block of memory is freed for reallocation. Trying to free
this block of memory can result in a segmentation fault.
Fix
The fix depends on the root cause of the defect. See if you intended to allocate a
memory block to the pointer between the first deallocation and the second.
Otherwise, remove the second free
statement.
As a good practice, after you free a memory block, assign the corresponding pointer to NULL. Before freeing pointers, check them for NULL values and handle the error. In this way, you are protected against freeing an already freed block.
Examples
Result Information
Group: Dynamic memory |
Language: C | C++ |
Default: On |
Command-Line Syntax: DOUBLE_DEALLOCATION |
Impact: High |
Version History
Introduced in R2013bSee Also
Find defects (-checkers)
| Use of previously freed pointer
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)