Windows下使用Gflags和UMDH查找内存泄漏
GFlags和UMDH与WinDbg一样,都是Debugging Tools for Windows里的工具。
1.设置符号路径
去微软官网下载对应的操作系统的符号安装文件,并安装到某个目录,如C:\WINDOWS\Symbols。
设置符号路径_NT_SYMBOL_PATH环境变量srv*C:/WINDOWS/Symbols*http://msdl.microsoft.com/download/symbols。
2.编写测试程序MemoryLeakTest
1 #include <iostream> 2 using namespace std; 3 4 int NewMemoryTest1() 5 { 6 char *p = new char[1024]; 7 memset(p, 0x00, 1024*sizeof(char)); 8 return 0; 9 } 10 11 int MallocMemoryTest1() 12 { 13 char *p = (char*)malloc(1024*sizeof(char)); 14 memset(p, 0x00, 1024*sizeof(char)); 15 return 0; 16 } 17 18 int main() 19 { 20 int i = 0; 21 cin >> i; 22 NewMemoryTest1(); 23 MallocMemoryTest1(); 24 cin >> i; 25 26 return 0; 27 }
3.打开Windows命令行窗口,在命令行中输入:GFlags /i MemoryLeakTest.exe +ust。
输出:
Current Registry Settings for MemoryLeakTest.exe executable are: 00001000
ust - Create user mode stack trace database
4.运行MemoryLeakTest.exe程序。
5.在命令行中输入:UMDH -pn:MemoryLeakTest.exe -f:D:\Snap1.txt。
6.在MemoryLeakTest.exe程序命令行中输输入任意数字,使程序执行NewMemoryTest1和MallocMemoryTest1函数。
7.在命令行中输入:UMDH -pn:MemoryLeakTest.exe -f:D:\Snap2.txt。
8.在命令行中输入:UMDH -d D:\Snap1.txt D:\Snap2.txt > D:\Result.txt。
在D:\Result.txt中有内存泄露的信息,可对比程序源代码进行分析。

浙公网安备 33010602011771号