boss44 发表于 2018-6-19 09:51:33

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
#include <iostream>  
using namespace std;
  
int NewMemoryTest1()
  
{
  
    char *p = new char;
  
    memset(p, 0x00, 1024*sizeof(char));
  
    return 0;
  
}
  
int MallocMemoryTest1()
  
{
  
    char *p = (char*)malloc(1024*sizeof(char));
  
    memset(p, 0x00, 1024*sizeof(char));
  
    return 0;
  
}
  
int main()
  
{
  
    int i = 0;
  
    cin >> i;
  
    NewMemoryTest1();
  
    MallocMemoryTest1();
  
    cin >> i;
  
    return 0;
  
}
  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中有内存泄露的信息,可对比程序源代码进行分析。
页: [1]
查看完整版本: Windows下使用Gflags和UMDH查找内存泄漏