How to detect memory leaks
Original text: https://my.oschina.net/u/4526289/blog/4539592
1. Memory leak problem principle
1.1 How heap memory is stored in C code
Memory leaks only occur when using heap memory. Stack memory does not have memory leaks because stack memory is automatically allocated and released. The heap memory allocation function in C code is malloc. Common memory allocation codes are as follows:
char *info = NULL; /**转换后的字符串**/
info = (char*)malloc(NB_MEM_SPD_INFO_MAX_SIZE);
if( NULL == info)
{
(void)tdm_error("malloc error!\n");
return NB_SA_ERR_HPI_OUT_OF_MEMORY;
}
Since the malloc function actually returns a memory address, the variable that stores heap memory must be a pointer (unless the code is extremely irregular).
To repeat, the variable that stores heap memory must be a pointer, which is very important for understanding the main point of this article. Of course, this pointer can be a single pointer or multiple pointers.
There are many variants or encapsulations of the malloc function, such as g_malloc, g_malloc0, VOS_Malloc, etc. These functions will eventually call the malloc function.
1.2 How to obtain heap memory
Seeing the title of this section, some students may be confused. Isn’t the malloc function in the previous section the method for obtaining heap memory?
Indeed, applying through the malloc function is the most direct way to obtain memory. If you only know this method of obtaining heap memory, you will easily fall into a trap. Generally speaking, there are two ways to obtain heap memory:
Method 1: Assign the function return value directly to the pointer. The general form is as follows:
char *local_pointer_xx = NULL;
local_pointer_xx = (char*)function_xx(para_xx, …);
This class involves memory allocation functions, and the return value is generally a pointer type, for example:
GSList* g_slist_append (GSList *list, gpointer data);
Method 2: Use the pointer address as the function return parameter and save the heap memory address through the return parameter. The general form is as follows:
int ret;
char *local_pointer_xx = NULL; /**转换后的字符串**/
ret = (char*)function_xx(..., &local_pointer_xx, ...);
This class involves memory allocation functions, which generally have a double pointer as an input parameter, for example:
__STDIO_INLINE _IO_ssize_t;
getline (char **__lineptr, size_t *__n, FILE *__stream);
As mentioned earlier, applying for memory through malloc is a specific manifestation of method 1. In fact, the essence of these two methods is the same, both of which indirectly apply for memory inside the function, but the method of passing memory is different. Method 1 passes the memory pointer through the return value, and method 2 passes the memory pointer through the parameter.
1.3 Three Elements of Memory Leak
The most common memory leak problems include the following three elements:
Factor 1: There is a local pointer variable definition in the function;
Element 2: The local pointer has memory obtained through one of the two heap memory acquisition methods in the previous section;
Element three: Before the function returns (including normal branches and exception branches), the memory is not released, saved to other global variables, or returned to the previous level function.
1.4 Memory Release Misunderstanding
Anyone who has used C language to write code should know that heap memory needs to be released after it is applied. But why is it so easy to have memory leaks?
On the one hand, it is caused by the developer's lack of experience, lack of awareness or negligence; on the other hand, it is caused by the misunderstanding of memory release. Many developers believe that the memory to be released should be limited to the following two types:
1) Directly use the memory requested by the memory request function, such as malloc, g_malloc, etc.;
2) In the interfaces that the developer is familiar with, there are memory requests, such as iBMC brothers. They should know that calling the following interface requires releasing the memory pointed to by the list:
dfl_get_object_list(const char* class_name, GSList **list);
If you write code according to the above thinking, once you encounter the problem of releasing memory in an unfamiliar interface, you will have no awareness of releasing memory at all, and memory leak problems will naturally arise.
2. How to check memory leak problem
The key to checking memory leaks is to develop good coding review habits.
To do the following three things:
1) If you see a local pointer in a function, be alert to memory leaks and develop the habit of further investigation
2) Analyze the assignment operation of the local pointer to see if it belongs to one of the "two methods of obtaining heap memory" mentioned above. If so, analyze what the pointer returned by the function points to?
Is it global data, static data, or heap memory? For unfamiliar interfaces, find the corresponding interface documentation or source code analysis; or check other references to the interface in the code to see if the memory is released;
3) If it is confirmed that there is a memory allocation operation for the local pointer, you need to analyze the destination of the memory. Will it be saved in a global variable? Or will it be used as a function return value? If neither is the case, you need to check all the places with "return" in the function to ensure that the memory is released correctly.
autumn
The recruitment has already begun. If you are not well prepared,
autumn
It is difficult to find a good job.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!