UMDH Check for memory leaks

Source: Internet
Author: User
Tags stack trace

1. Download the debug Tools for windows:http://www.microsoft.com/whdc/devtools/debugging/default.mspx (inside the UMDH is used to check for memory leaks), installation, and set the installation path to the environment variable (easy to start on the command line)
2. Write test program test.exe, such as a thread to constantly allocate memory, not too violent, the smoke of your machine, mother will scold ~
3. Set the path of the symbol set (symbols file, that is,. pdb files) to the _NT_SYMBOL_PATH environment variable, you can directly specify the test program's Build directory (VC-generated PDB files are put together with executable files)
4. Start the test program heap trace: Gflag-i test.exe +ust, Gflag is also a tool in debug tools for Windows to start or close advanced debugging and tracking of a process, which is recorded in the registry
5. Start the test program Test.exe, record its process PID number, you can use Task Manager to see, you can also execute the command: tlist | Findstr Test.exe, for example: 1884
6. Start recording the initial log, execute the command: Umdh-p:1884-f:1884old.log, so that the leak operation before the heap state records;
7. Continue to execute the test procedure, let him produce a leak
8. Start recording end log, execute command: Umdh-p:1884-f:1884new.log, so that the leak after the operation of the Heap state records;
9. Execute comparison command: umdh-d 1884old.log 1884new.log > Cmp1884.txt, write output to Cmp1884.txt file.

If the PDB file path is configured correctly, information similar to the following is logged in Cmp1884.txt:

===================================================================
+ 10600 (10600-0) Allocs backtrace4a
+ ten (10-0) BACKTRACE4A Allocations

ntdll! Rtlpntmaketemporarykey+000074ce
ntdll! Ldralternateresourcesenabled+00002afd
ntdll! rtldossearchpath_ustr+00000310
Msvcr90d!malloc_base+000000fe
msvcr90d!malloc_dbg+00000306
Msvcr90d!malloc_dbg+000000bf
msvcr90d!malloc_dbg+0000006c
msvcr90d!malloc+0000001b
Msvcr90d!operator new+00000011
testumdh! receivingthrd+00000040 (E:\code\testumdh\test.cpp, 27)
msvcr90d!beginthreadex+00000243
Msvcr90d!beginthreadex+000001d9
kernel32! Getmodulefilenamea+000001ba
====================================================================

This is the allocation of memory when the call stack, but also pointed out that the memory allocation of the specific function name and code line, the rest is the physical life

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------

UMDH is a tool in debugging tools for Windows that can download http://www.microsoft.com/whdc/devtools/debugging/default.mspx from the link below. UMDH mainly discovers memory leaks by analyzing the heap Stack trace information of the comparison process.before using UMDH 1. Set _NT_SYMBOL_PATH environment variables, such as using the command line:Set _nt_symbol_path=c:/windows/symbols。 Put your own program's SYMBOL files (. pdb) files in the same directory as you execute the files, or add them to the _NT_SYMBOL_PATH environment variables. 2. Set gflags, by commandgflags-i notepad.exe +ust, GFlags is also debugging Tools for Windows inside a tool program. You can also type gflags command, and then through the interface configuration, enter the interface after the choice of image file, in the image column to write the name of the execution file, do not need a full path, such as as long as the input notepad.exe, and then press the TAB key to select Create User mode Stack trace database option, confirm.Dump to capture the heap 1. Get the process number of the program you want to analyze, such as your process number is 1234, enter at the command lineumdh-p:1234-f: 1234old.logTo get the 1234old.log file. 2. Continue running your program, or do something that you suspect will have a memory leak. 3. After an interval of time, enter the commandumdh-p:1234-f: 1234new.log。 4. And then runumdh-d 1234old.log 1234new.log > Cmp1234.txtAnalysis Comparison Results1.cmp1234.txt is the difference between heap Stack trace at two moments, which resembles the following information: + 5320 (F110-9DF0) 3a Allocs BackTrace00053
Total Increase = = 5320

2. The next step is to find the corresponding backtrace, for example, the above means that the memory in the BackTrace00053 increased by 5,320 bytes, in BackTrace00053 you will be able to find the memory leakage of the corresponding callstack.

3. Let's take a look at what BackTrace00053 actually has, find the second log file, here is 1234new.log, search BackTrace00053, if your symbol File path is configured correctly, In BackTrace00053 you will find a message similar to the following: 00005320 bytes in 0x14 allocations (@ 0x00000428) by:backtrace00053 RTLDEBUGALLOCATEHEAP+0X000000FD ntdll! rtlallocateheapslowly+0x0000005a ntdll! rtlallocateheap+0x00000808 myapp!_heap_alloc_base+0x00000069 myapp!_heap_alloc_dbg+0x000001a2 MyApp!_nh_malloc_ dbg+0x00000023 myapp!_nh_malloc+0x00000016 myapp!operator new+0x0000000e myapp! leakyfunc+0x0000001e myapp!main+0x0000002c MYAPP!MAINCRTSTARTUP+0X000000FC kernel32! baseprocessstart+0x0000003d above is the stack trace information that allocates that memory, where we see the real myapp! The LeakyFunc function has a new operation inside it. The above information shows that within two log intervals, myapp!      LeakyFunc has allocated new memory, but has not yet been released. The link below is a video about UMDH: http://wm.microsoft.com/ms/mcsp/servicedesk/080702.wmv. X

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.