Skip to main content

IAR Embedded Workbench for RL78 5.20

MISRAC2012-Dir-4.13_b

In this section:
Synopsis

(Advisory) Functions which are designed to provide operations on a resource should be called in an approriate sequence. Memory is allocated, but then the pointer value is lost due to reassignment or its scope ending, without a guarantee of the value being propagated or the memory being freed. There must be no possible execution path during which the value is not freed, returned, or passed into another function as an argument, before it is lost. This is a memory leak.

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Incorrect deallocation causes memory leak. This check is identical to MEM-leak-alias.

Coding standards
MISRA C:2012 Dir-4.13

(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence

Code examples

The following code example fails the check and will give a warning:

#include <stdlib.h>

int main(void) {
  int *ptr = (int *)malloc(sizeof(int));
  
  ptr = NULL; //losing reference to the allocated memory

  free(ptr);

  return 0;
}

The following code example passes the check and will not give a warning about this issue:

#include <stdlib.h>

int main(void) {
    int *ptr = (int*)malloc(sizeof(int));
    if (rand() < 5) {
        free(ptr);
    } else {
        free(ptr);
    }
    return 0;
}