Invalid free of pointer
Pointer deallocation without a corresponding dynamic allocation
Description
This defect occurs
when a block of memory released using the free
function
was not previously allocated using malloc
, calloc
,
or realloc
.
Risk
The free
function releases a block of memory allocated on the
heap. If you try to access a location on the heap that you did not allocate
previously, a segmentation fault can occur.
The issue can highlight coding errors. For instance, you perhaps wanted to use the
free
function or a previous malloc
function on a different pointer.
Fix
In most cases, you can fix the issue by removing the free
statement. If the pointer is not allocated memory from the heap with
malloc
or calloc
, you do not need to free
the pointer. You can simply reuse the pointer as required.
If the issue highlights a coding error such as use of free
or
malloc
on the wrong pointer, correct the error.
If the issue occurs because you use the free
function to free
memory allocated with the new
operator, replace the
free
function with the delete
operator.
Examples
Result Information
Group: Dynamic Memory |
Language: C | C++ |
Default: On |
Command-Line Syntax: BAD_FREE |
Impact: High |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Invalid deletion of 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)