CWE Rule 459
Description
The product does not properly "clean up" and remove temporary or supporting resources after they have been used.
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: Initialization and Cleanup Errors |
PQL Name:
std.cwe_native.R459 |
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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)