Static Analysis Problem Type Reference

Memory leak

Dynamically allocated memory was not freed.

Memory leaks are detected when the last pointer to a piece of dynamically allocated memory is destroyed without that value having been freed. Pointer values are destroyed when the variable is overwritten or when its declaration goes out of scope, either by leaving a subroutine or a block.

ID

Code Location

Description

1

Memory leak

The place where the memory was leaked

2

Allocation site

The place where the memory was allocated

Example

          
#include <stdlib>

char *p1;
H
void f()
{
    char *p2;
    p1 = (char *)malloc(10); // allocated here
    p2 = p1;
    
    p1 = 0;
    // value in p1 is destroyed, but memory is not leaked yet,
    // because p2 still contains address and p2 could be freed
    
    *p2 = 'a';
    // memory is leaked here because p2 is destroyed when f returns
}