The check of Memory leakage is essentially to locate Memory leakage. This positioning includes two meanings:
The first is the positioning module, that is, the module that has encountered a problem.
Second, locate the code, that is, find the code that causes memory leakage.
In this regard, in the context of division of labor and cooperation to complete a system, we should check the memory leakage workflow: first, locate the module; second, if there is a problem with our module, locate the code.
Checking for memory leaks is more like a detective. Only by finding enough evidence can we find the real cause. In order to become a good detective, the reconnaissance capability is the most important. Good detectives generally have some good detective tools.
So how should we evaluate the advantages and disadvantages of the memory leak check tool? Here, let's imagine:
1) It can freely choose the monitoring time point;
2) It can attach any running process to track memory leaks;
3) I do not need to add any code in my program;
4) without source code, I can still find memory leaks;
5) I can get the heap state of memory allocation but not released each time.
6) whether it is the release version or the debugging version of the program, the above points can be achieved.
Here, we recommend that you use leakdiag, a tool developed by Microsoft.
Leakdiag is a tool used to monitor memory leaks. It can be used to precisely locate memory leaks until the code line. It uses Microsoft's detours technology to intercept calls with specified memory allocation, track various call stacks, and report allocated but not released memory, this information allows us to precisely check which components are allocated when we eliminate a memory leak. With the correct debugging symbol, we can even see the code lines allocated by the request. Compared with various memory leak detection tools on the market, I think this tool has better achieved the above six points.
Leakdiag supports five different distributions:
1) virtual allocation. This allocation can trace the memory allocated by virtual memory, such as virtualalloc/virtualallocex. In this case, select the "Virtual Memory Allocator" option of [Memory allocators] On leakdiag to detect.
2) Heap allocation. This allocation can track the memory allocated by the following functions in Ntdll. dll.
· Rtlcreateheap,
· Rtldestroyheap,
· Rtlallocateheap,
· Rtlfreeheap,
· Rtlreallocateheap,
· Localrealloc,
· Localfree,
· Localalloc,
· Localrealloc,
· Globalalloc,
· Globalrealloc and
· Globalfree
In this case, select "Windows heap Allocator" for the [Memory allocators] Option on leakdiag to detect the event.
3) Thread Local Storage (TLS refers to a Win32 mechanism that allows multiple threads of a process to store data unique to each thread) allocation. This allocation can track the memory allocated by the following functions in msdart32.dll.
· Mpheapalloc,
· Mpheapfree and
· Mpheaprealloc
In this case, select "mpheap Allocator" for the [Memory allocators] Option on leakdiag to detect the event.
4) com allocation (external and internal ). This allocation can be traced to ole32.dll, and the memory allocated by the following function in oleaut32.dll.
· Cogetmalloc,
· Cotaskmemalloc,
· Cotaskmemfree,
· Cotaskmemrealloc,
· Cretailmalloc_alloc,
· Cretailmalloc_free,
· Cretailmalloc_realloc,
· Sysallocstringlen,
· Sysallocstringbytelen,
· Sysallocstring,
· Sysfreestring,
· Sysreallocstring and
· Sysreallocstringlen
In this case, select "com Allocator" or "com internal Allocator" for the [Memory allocators] Option on leakdiag to detect the event.
5) C is allocated during runtime. This allocation can track the memory allocated by the following functions in msvcrt. dll.
· Malloc,
· Calloc,
· Realloc,
· Free,
· New,
· New [],
· Delete and
· Delete []
In this case, select "C Runtime Allocator" for the [Memory allocators] Option on leakdiag to detect the event.