1. 检测需要使用的工具: windbg 工具.
检测前, 需要先安装 windbg 工具. 安装了该工具后, 会在安装目录下有一个 umdh 工具.
假设 windbg 安装在以下目录下: D:\Program Files\Debugging Tools for Windows (x86)
2. 内存泄露检测技巧
2.1 运行 cmd 窗口, 将路径设置到安装路径下;
2.2 在该 cmd 窗口中设置环境变量_NT_SYMBOL_PATH:
D:\Program Files\Debugging Tools for Windows (x86)>set _NT_SYMBOL_PATH="%windir%\symbols";D:\AppPath; // 说明: AppPath 为应用程序绝对路径
2.3 在该 cmd 窗口中设置标签 flags:
D:\Program Files\Debugging Tools for Windows (x86)>gflags -i appname.exe +ust
2.4 启动应用程序 appname.exe;
2.5 保存 appname.exe 的当前状态:
D:\Program Files\Debugging Tools for Windows (x86)>umdh.exe -pn:appname.exe -f:FirstDump.txt
2.6 间隔一段时间后, 继续保存 appname.exe 的当前状态:
D:\Program Files\Debugging Tools for Windows (x86)>umdh.exe -pn:appname.exe -f:SecondDump.txt
2.7 使用 umdh.exe 工具比较两个 dump 文件:
D:\Program Files\Debugging Tools for Windows (x86)>umdh.exe FirstDump.txt SecondDump.txt -f:Result.txt
2.8 最后, 通过打开 result.exe 文件, 就可以看到里面显示了代码行中的那个语句出现了内存泄露.
来源: http://www.bubuko.com/infodetail-2806042.html