About pointer leakage

xiaoxiao2021-03-06  38

For a C / C programmer, memory leaks is a common and headache problem. There have been many technologies to be studied to deal with this problem, such as Smart Pointer, Garbage Collection, etc. Smart Pointer technology is relatively mature, and the STL already contains Class of Smart Pointer, but its use does not seem broad, and it does not solve all the problems; Garbage Collection technology is more mature in Java, but in C / C field The development is not smooth, although some people think about the support of GC in C . The real world is like this, as a C / C programmer, memory leak is your feelings in your heart. However, in now there are many tools to help us verify the existence of memory leaks, find out the code that has problems. The definition of memory leaks generally said that the memory leakage is the leakage of the stack memory. The stack memory is that the program is allocated from the heap, the size is arbitrary (the size of the memory block can be determined in the program running period), and the released memory must be displayed after use. Applications generally allocate functions such as Malloc, Realloc, New, from the heap, after use, the program must be responsible for the corresponding call free or delete release the memory block, otherwise, this memory cannot be used again, we will Said this memory leak. The following small program demonstrates the situation in which a pile of memory leaks: void myfunction (int nsize) {char * p = new char [nsize]; if (! GetStringFrom (p, nsize)) {MessageBox ("Error"); Return } ... // using the string pointed by p; delete p;} When the function getStringFrom () returns zero, the pointer P pointing to the memory will not be released. This is a common situation in which memory leaks occur. The program allocates memory at the entrance, releases memory at the exit, but the C function can exit anywhere, so once the memory that should be released at an exit, the memory leak will occur. Generalized saying that memory leaks not only contains leaks of stacks, but also contains resource leaks, such as core states, GDI Object, Socket, Interface, etc., fundamentally, these objects allocated by the operating system are also The memory is consumed, and if these objects, the leakage will ultimately lead to the leakage of memory. Moreover, some object consumes core state memory, which will cause the entire operating system unstable when it is severely leaked. Therefore, the leakage of system resources is more serious than the leakage of the stack memory. GDI Object Leak is a common resource leak: void cmyview :: onpaint (cdc * pdc) {cbitmap bmp; cbitmap * PoldBMP; bmp.loadbitmap; PoldBMP = PDC-> SelectObject (& BMP); ... IF Something ()) {Return;} PDC-> SelectObject (PoldBMP); Return;} Example 2 When the function Something () When returned, the program did not select the PoldBMP back to the PDC before exiting, which causes PoldBMP points to Hbitmap object leaks.

If this program is run for a long time, it may cause the entire system. This problem is easier to expose under Win9X because Win9x's GDI stack is much smaller than Win2K or NT. Memory leakage happens: Classification in a way, memory leaks can be divided into 4 categories: 1. Avantime memory leakage. The code that occurs in memory leaks will be executed multiple times, and a memory leak will be caused each time it is executed. For example, if the Something () function has returned True, the HbitMap object points to PoldBMP always leaks. 2. Appetitive memory leak. The code that occurs in memory leaks only occurs under certain specific environments or operations. For example, if the Something () function returns TRUE in a particular environment, the HBitMap object pointing to PoldBMP does not always leak. Avantages and occasionalities are relative. For a specific environment, the suspended may become a normal. So the test environment and test methods are critical to detect memory leaks. 3. Disposable memory leakage. The code that has a memory leak will only be executed once, or due to the defects on the algorithm, there will always be only a piece of only and one memory leak. For example, allocate memory in the constructor of the class, but does not release the memory in the destructuring function, but because this class is a Singleton, the memory leak will only occur once. Another example: char * g_lpszFileName = NULL; void SetFileName (const char * lpcszFileName) {if (g_lpszFileName) {free (g_lpszFileName);} g_lpszFileName = strdup (lpcszFileName);} if the program according to a third g_lpszFileName not released at the end point String, then, even if setFileName () multiple times, there will always be a memory, and only one memory is leaking. 4. Implicit memory leaks. The program is constantly allocated during the running process, but the memory is released until the end. Strictly speaking, there is no memory leak, because the final program releases all application memory. But for a server program, it is necessary to run a few days, several weeks or even months, and the memory may not be released in time may result in all memory of the final depletion system. Therefore, we call such memory leaks for implicit memory leaks.

转载请注明原文地址:https://www.9cbs.com/read-63290.html

New Post(0)