`
kuwoleft
  • 浏览: 1076778 次
文章分类
社区版块
存档分类
最新评论

内存泄漏测试小工具 - LeakDiag

 
阅读更多

LeakDiag是一个监测内存泄漏的工具,可以用来精确地找到内存泄露一直到代码行。它使用微软的Detours 技术,拦截指定内存分配的调用并跟踪各种调用栈,并报告已分配但尚未释放的内存,这一信息允许让我们在排除一个内存泄露问题时,能精确查看哪些组件进行了该分配。使用正确的调试符号,我们甚至可以看见请求分配的代码行。

关于detours http://research.microsoft.com/en-us/projects/detours/

LeakDiag 目前支持六种类型的泄漏检查:

Virtual Allocator

Heap Allocator

MPHeap Allocator

COM AllocatorCoTaskMem

COM Private Allocator

C Runtime Allocator

版本:1.25

下载地址:

ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20Tools/LeakDiag/

或者:

http://d.download.csdn.net/down/1168689/cloveroger

使用方法:

1、编写一个包含内存泄漏问题的C程序,例如LeakDiagTest1

#include <stdlib.h>

int main(int argc, char* argv[])

{

char *p = NULL;

while (getc(stdin))

{

p = (char *)malloc(1024);

//delete(p);

}

return 0;

}

2、运行C程序的可执行文件LeakDiagTest1.exe,打开LeakDiag

在进程列表中选择LeakDiagTest1.exe

Memory allocators列表中选择“Windows Heap Allocator

Start开始进行监测

3、在LeakDiagTest1运行过程中,可多次按Log进行内存信息收集(“快照”),最后按Stop停止监测

4、打开logs目录中的日志文件进行分析,可发现有内存泄漏现象:

<FRAME num="0" dll="LeakDiagTest1.exe" function ="_heap_alloc_base" offset="0x55" filename="malloc.c" line="161" addr="0x4035F5" />

<FRAME num="1" dll="LeakDiagTest1.exe" function ="_heap_alloc_dbg" offset="0x1A2" filename="dbgheap.c" line="367" addr="0x401352" />

<FRAME num="2" dll="LeakDiagTest1.exe" function ="_nh_malloc_dbg" offset="0x19" filename="dbgheap.c" line="242" addr="0x401159" />

<FRAME num="3" dll="LeakDiagTest1.exe" function ="malloc" offset="0x18" filename="dbgheap.c" line="130" addr="0x4010E8" />

<FRAME num="4" dll="LeakDiagTest1.exe" function ="main" offset="0x76" filename="E:/tmp/LeakDiagTest1/LeakDiagTest1.cpp" line="13" addr="0x401086" />

5、用LDGrapher可以图形的方式更加直观地看到多个快照点连续的内存泄漏情况

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics