In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to solve the memory leak in C language, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
1. Preface
Recently, there have been a series of online problems caused by memory leaks in different products of different departments, such as the veneer reset phenomenon caused by memory exhaustion after several months of running on the current network. On the one hand, the memory leak problem is a low-level error, and this kind of problem is omitted to the existing network, and the impact is very bad; on the other hand, the memory leak problem is likely to cause the veneer to reset after a fixed time, which can only be solved through batch upgrades. The actual impact is also very bad. At the same time, there are such problems one after another, especially one of which is modified and introduced by our old staff, which shows that many of our employees do not have a deep understanding of the memory leak problem. By introducing the principle and inspection method of memory leakage, this paper hopes to put an end to this kind of problem from the coding inspection link.
Note: there are many ways to prevent memory leaks, such as enhanced code review, tool detection and memory testing. This article focuses on improving the ability of developers.
two。 Principle of memory leak 2.1 Storage in C code in heap
Memory leaks occur only when heap memory is used, and there is no memory leak in stack memory because stack memory is automatically allocated and freed. The heap memory request function in C code is malloc. The common memory request code is as follows:
Char* info = NULL; / * * converted string * * / 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;}
Because the malloc function actually returns a memory address, the variable that holds the heap memory must be a pointer (unless the code is extremely irregular). Again, the variable that holds the heap memory must be a pointer, which is important for understanding the gist of this article. Of course, this pointer can be a single pointer or multiple pointers.
There are many variants or wrappers of malloc functions, such as g_malloc, g_malloc0, VOS_Malloc and so on. These functions will eventually call the malloc function.
2.2 the method of obtaining heap memory
Seeing the title of this section, some students may be confused. Isn't the malloc function in the previous section the way to get heap memory? Indeed, applying through the malloc function is the most direct way to get it, and if you only know this method of getting heap memory, it's easy to fall into a pit. Generally speaking, there are two ways to get heap memory:
Method 1: assign the return value of the function directly to the pointer, which is generally shown as follows:
Char* local_pointer_xx = NULL;local_pointer_xx = (char*) function_xx (para_xx, …)
This class involves the function of memory request, and the return value is generally of pointer type, for example:
GSList* g_slist_append (GSList* list, gpointer data)
Method 2: take the pointer address as the function return parameter, and save the heap memory address through the return parameter, which is generally shown as follows:
Int ret; char* local_pointer_xx = NULL; / * * converted string * * / ret = (char*) function_xx (..., & local_pointer_xx,...)
This class involves the function of memory request. Generally, one input parameter is a double pointer, for example:
_ _ STDIO_INLINE _ IO_ssize_tgetline (char * * _ lineptr, size_t * _ n, FILE * _ _ stream)
As mentioned earlier, applying for memory through malloc belongs to a specific form of method 1. In fact, the essence of these two kinds of methods is the same, both indirectly apply for memory within the function, but the method of transferring memory is different. Method 1 passes the memory pointer through the return value, and method 2 passes the memory pointer through parameters.
2.3 three elements of memory leakage
The most common memory leak problems include the following three elements:
Element 1: local pointer variables are defined in the function
Element 2: the local pointer can be obtained by one of the "two heap memory acquisition methods" in the previous section
Element 3: the memory is not released before the function returns (including normal branches and exception branches), and it is not saved to other global variables or returned to the function at a higher level.
2.4 misunderstanding of memory release
Anyone who has written code in C should know that heap memory needs to be freed after the request. But why are memory leaks so easy? On the one hand, it is caused by the lack of experience, awareness or temporary negligence of developers; on the other hand, it is caused by the misunderstanding of memory release. Many developers believe that the memory to be freed should be limited to the following two types:
1) directly use the memory applied for by the memory request function, such as malloc, g_malloc, etc.
2) in the APIs familiar to the developer, memory requests, such as iBMC's brothers, should know that the memory pointed to by list needs to be freed when calling the following APIs:
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 the problem of memory leak will occur naturally.
3. Inspection method of memory leak problem
The key to examining memory leaks is to develop good coding viewing habits. Corresponding to the three elements of memory leak, need
To achieve the following three points:
(1) if you see a local pointer in a function, you should be on guard against memory leaks and form the habit of further investigation.
(2) analyze whether the assignment of the local pointer belongs to one of the "two heap memory acquisition methods" mentioned above, and if so, what exactly does the pointer returned by the function point to? Is it global data, static data, or heap memory? For unfamiliar interfaces, find the corresponding interface documentation or source code analysis, or see if memory is freed by references to the interface elsewhere in the code
(3) if you confirm that there is a memory request operation for the local pointer, you need to analyze the direction of the memory. Will it be saved in the global variable? Or will it be returned as a function? If not, you need to check all the places with "return" in the function to make sure that the memory is freed correctly.
On the C language memory leak how to solve here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.