CWE Rule 771
Description
The product does not properly maintain a reference to a resource that has been allocated, which prevents the resource from being reclaimed.
Polyspace Implementation
The rule checker checks for:
Resource leak
Memory leak
.
Examples
This issue occurs when you open a file stream by using a FILE pointer but do not close it before:
The end of the pointer's scope.
Assigning the pointer to another stream.
If you do not release file handles explicitly as soon as possible, a failure can occur due to exhaustion of resources.
Close a FILE pointer before the end of its scope, or before you assign the pointer to another stream.
#include <stdio.h>
void func1( void ) {
FILE *fp1;
fp1 = fopen ( "data1.txt", "w" );
fprintf ( fp1, "*" );
fp1 = fopen ( "data2.txt", "w" ); //Noncompliant
fprintf ( fp1, "!" );
fclose ( fp1 );
}In this example, the file pointer fp1 is pointing to a file data1.txt. Before fp1 is explicitly dissociated from the file stream of data1.txt, it is used to access another file data2.txt.
FILE PointerOne possible correction is to explicitly dissociate fp1 from the file stream of data1.txt.
#include <stdio.h>
void func1( void ) {
FILE *fp1;
fp1 = fopen ( "data1.txt", "w" );
fprintf ( fp1, "*" );
fclose(fp1);
fp1 = fopen ( "data2.txt", "w" );
fprintf ( fp1, "!" );
fclose ( fp1 );
}
This issue occurs when you do not free a block of memory allocated through malloc, calloc, realloc, or new. If the memory is allocated in a function, the defect does not occur if:
Within the function, you free the memory using
freeordelete.The function returns the pointer assigned by
malloc,calloc,realloc, ornew.The function stores the pointer in a global variable or in a parameter.
Dynamic memory allocation functions such as malloc allocate memory on the heap. If you do not release the memory after use, you reduce the amount of memory available for another allocation. On embedded systems with limited memory, you might end up exhausting available heap memory even during program execution.
Determine the scope where the dynamically allocated memory is accessed. Free the memory block at the end of this scope.
To free a block of memory, use the free function on the pointer
that was used during memory allocation. For
instance:
ptr = (int*)malloc(sizeof(int));
...
free(ptr);It is a good practice to allocate and free memory in the same module at the same level
of abstraction. For instance, in this example, func allocates and frees
memory at the same level but func2 does
not.
void func() {
ptr = (int*)malloc(sizeof(int));
{
...
}
free(ptr);
}
void func2() {
{
ptr = (int*)malloc(sizeof(int));
...
}
free(ptr);
}In this example, pi is dynamically allocated by malloc. The function assign_memory does not free the memory, nor does it return pi.
#include<stdlib.h>
#include<stdio.h>
void assign_memory(void)
{
int* pi = (int*)malloc(sizeof(int));
if (pi == NULL)
{
printf("Memory allocation failed");
return;
}
*pi = 42;
/* Defect: pi is not freed */
} // NoncompliantOne possible correction is to free the memory referenced by pi using the free function. The free function must be called before the function assign_memory terminates
#include<stdlib.h>
#include<stdio.h>
void assign_memory(void)
{
int* pi = (int*)malloc(sizeof(int));
if (pi == NULL)
{
printf("Memory allocation failed");
return;
}
*pi = 42;
/* Fix: Free the pointer pi*/
free(pi);
}Check Information
| Category: Resource Management Errors |
PQL Name: std.cwe_native.R771 |
Version History
Introduced in R2026a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)