使用 Windbg 检测内存泄漏






4.80/5 (53投票s)
使用 Windbg 检测内存泄漏。
引言
内存泄漏是 C++ 开发者经常遇到的耗时 bug。检测内存泄漏通常很繁琐。如果代码不是你写的,或者代码库非常庞大,情况会变得更糟。
虽然市面上有一些工具可以帮助你检测内存泄漏,但大多数工具都不是免费的。我发现 Windbg 是一款免费且功能强大的工具,可以解决内存泄漏的 bug。至少,我们可以了解可能导致内存泄漏的代码位置。COM 接口泄漏不在本文的讨论范围之内。
Windbg 是微软提供的一款强大的用户/内核空间调试器,可以从这里下载和安装。
使用 Windbg
开始使用 Windbg
- 将符号文件路径配置到 Microsoft 符号服务器“SRV*d:\symbols*http://msdl.microsoft.com/download/symbols”。
- 将你的程序 EXE/DLL PDB(程序数据库)路径添加到符号文件路径。
- 你还需要配置操作系统的标志,以启用出现内存泄漏进程的用户堆栈跟踪。这很简单,可以使用 gflags.exe 完成。Gflags.exe 在 Windbg 的安装过程中安装。也可以通过命令行使用命令“gflags.exe /i MemoryLeak.exe +ust”来完成。我的程序名称是 Test2.exe;因此,为了演示,我将使用 Test2.exe 而不是 MemoryLeak.exe。下面的快照显示了应用程序 Test2.exe 的操作系统标志设置。
为符号文件路径配置 Windbg 后,启动正在泄漏内存的进程,并将 Windbg 附加到它。 Windbg 中的“附加”选项位于“文件”菜单下,或者可以使用 F6 快捷键启动。下面的快照显示了相同的内容。
Windbg 的 !heap 命令用于显示堆。 Windbg 的帮助文档中对 !heap 进行了详细说明。
我开发了一个小的程序,该程序会泄漏内存,并将使用该程序进一步演示。
int _tmain(int argc, _TCHAR* argv[])
{ while(1)
{
AllocateMemory();
}
return 0;
}
void AllocateMemory()
{
int* a = new int[2000];
ZeroMemory(a, 8000);
Sleep(1);
}
上面的程序泄漏了一个大小为 2000*4 字节的整数数组。
将 Windbg 附加到进程后,执行 !heap –s 命令。 -s 代表 summary(摘要)。下面是泄漏进程的 !heap -s 的输出。
0:001> !heap -s
NtGlobalFlag enables following debugging aids for new heaps:
validate parameters
stack back traces
Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast
(k) (k) (k) (k) length blocks cont. heap
-----------------------------------------------------------------------------
00150000 58000062 1024 12 12 1 1 1 0 0 L
00250000 58001062 64 24 24 15 1 1 0 0 L
00260000 58008060 64 12 12 10 1 1 0 0
00330000 58001062 64576 47404 47404 13 4 1 0 0
-----------------------------------------------------------------------------
让进程执行一段时间,然后重新中断到进程中,再次执行 !heap -s。下面显示的是该命令的输出。
0:001> !heap -s
NtGlobalFlag enables following debugging aids for new heaps:
validate parameters
stack back traces
Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast
(k) (k) (k) (k) length blocks cont. heap
-----------------------------------------------------------------------------
00150000 58000062 1024 12 12 1 1 1 0 0 L
00250000 58001062 64 24 24 15 1 1 0 0 L
00260000 58008060 64 12 12 10 1 1 0 0
00330000 58001062 261184 239484 239484 14 4 1 0 0
-----------------------------------------------------------------------------
以粗体标记的行显示了增长的堆。上面的快照显示了句柄为 00330000 的堆在增长。
对增长的堆执行 “!heap -stat –h 00330000”。此命令显示增长的堆的堆统计信息。下面显示的是该命令的输出。
0:001> !heap -stat -h 00330000
heap @ 00330000
group-by: TOTSIZE max-display: 20
size #blocks total ( %) (percent of total busy bytes)
1f64 76c6 - e905f58 (99.99)
1800 1 - 1800 (0.00)
824 2 - 1048 (0.00)
238 2 - 470 (0.00)
244 1 - 244 (0.00)
4c 5 - 17c (0.00)
b0 2 - 160 (0.00)
86 2 - 10c (0.00)
50 3 - f0 (0.00)
74 2 - e8 (0.00)
38 4 - e0 (0.00)
48 3 - d8 (0.00)
c4 1 - c4 (0.00)
62 2 - c4 (0.00)
be 1 - be (0.00)
b8 1 - b8 (0.00)
ae 1 - ae (0.00)
ac 1 - ac (0.00)
55 2 - aa (0.00)
a4 1 - a4 (0.00)
上面的快照显示了 0x76c6 个大小为 1f64 的块正在被分配(以粗体标记)。如此大量的相同大小的块使我们怀疑这些可能是泄漏的块。其余的块分配没有增长的块数。
下一步是获取这些块的地址。使用命令 !heap -flt s 1f64。此命令过滤掉堆的所有其他块,并显示大小为 1f64 的块的详细信息。
下面显示的是该命令的输出。
0:001> !heap -flt s 1f64
_HEAP @ 150000
_HEAP @ 250000
_HEAP @ 260000
_HEAP @ 330000
HEAP_ENTRY Size Prev Flags UserPtr UserSize - state
003360e0 03f0 0000 [07] 003360e8 01f64 - (busy)
00338060 03f0 03f0 [07] 00338068 01f64 - (busy)
00339fe0 03f0 03f0 [07] 00339fe8 01f64 - (busy)
0033bf60 03f0 03f0 [07] 0033bf68 01f64 - (busy)
0033dee0 03f0 03f0 [07] 0033dee8 01f64 - (busy)
01420040 03f0 03f0 [07] 01420048 01f64 - (busy)
01421fc0 03f0 03f0 [07] 01421fc8 01f64 - (busy)
01423f40 03f0 03f0 [07] 01423f48 01f64 - (busy)
01425ec0 03f0 03f0 [07] 01425ec8 01f64 - (busy)
01427e40 03f0 03f0 [07] 01427e48 01f64 - (busy)
01429dc0 03f0 03f0 [07] 01429dc8 01f64 - (busy)
0142bd40 03f0 03f0 [07] 0142bd48 01f64 - (busy)
0142dcc0 03f0 03f0 [07] 0142dcc8 01f64 - (busy)
0142fc40 03f0 03f0 [07] 0142fc48 01f64 - (busy)
01431bc0 03f0 03f0 [07] 01431bc8 01f64 - (busy)
01433b40 03f0 03f0 [07] 01433b48 01f64 - (busy)
01435ac0 03f0 03f0 [07] 01435ac8 01f64 - (busy)
01437a40 03f0 03f0 [07] 01437a48 01f64 - (busy)
014399c0 03f0 03f0 [07] 014399c8 01f64 - (busy)
0143b940 03f0 03f0 [07] 0143b948 01f64 - (busy)
0143d8c0 03f0 03f0 [07] 0143d8c8 01f64 - (busy)
0143f840 03f0 03f0 [07] 0143f848 01f64 - (busy)
014417c0 03f0 03f0 [07] 014417c8 01f64 - (busy)
01443740 03f0 03f0 [07] 01443748 01f64 - (busy)
014456c0 03f0 03f0 [07] 014456c8 01f64 - (busy)
01447640 03f0 03f0 [07] 01447648 01f64 - (busy)
014495c0 03f0 03f0 [07] 014495c8 01f64 - (busy)
0144b540 03f0 03f0 [07] 0144b548 01f64 - (busy)
0144d4c0 03f0 03f0 [07] 0144d4c8 01f64 - (busy)
0144f440 03f0 03f0 [07] 0144f448 01f64 - (busy)
014513c0 03f0 03f0 [07] 014513c8 01f64 - (busy)
01453340 03f0 03f0 [07] 01453348 01f64 - (busy)
014552c0 03f0 03f0 [07] 014552c8 01f64 - (busy)
01457240 03f0 03f0 [07] 01457248 01f64 - (busy)
014591c0 03f0 03f0 [07] 014591c8 01f64 - (busy)
0145b140 03f0 03f0 [07] 0145b148 01f64 - (busy)
0145d0c0 03f0 03f0 [07] 0145d0c8 01f64 - (busy)
0145f040 03f0 03f0 [07] 0145f048 01f64 - (busy)
01460fc0 03f0 03f0 [07] 01460fc8 01f64 - (busy)
01462f40 03f0 03f0 [07] 01462f48 01f64 - (busy)
01464ec0 03f0 03f0 [07] 01464ec8 01f64 - (busy)
01466e40 03f0 03f0 [07] 01466e48 01f64 - (busy)
01468dc0 03f0 03f0 [07] 01468dc8 01f64 - (busy)
从列出的输出中使用任何 UsrPtr 列值,然后使用命令 !heap -p -a UsrPtr 显示 UsrPtr
的调用堆栈。我选择了以粗体标记的 0143d8c8。
执行 !heap -p -a 0143d8c8 后,我们得到如下所示的调用堆栈。
0:001> !heap -p -a 0143d8c8
address 0143d8c8 found in
_HEAP @ 330000
HEAP_ENTRY Size Prev Flags UserPtr UserSize - state
0143d8c0 03f0 0000 [07] 0143d8c8 01f64 - (busy)
Trace: 0025
7c96d6dc ntdll!RtlDebugAllocateHeap+0x000000e1
7c949d18 ntdll!RtlAllocateHeapSlowly+0x00000044
7c91b298 ntdll!RtlAllocateHeap+0x00000e64
102c103e MSVCR90D!_heap_alloc_base+0x0000005e
102cfd76 MSVCR90D!_heap_alloc_dbg_impl+0x000001f6
102cfb2f MSVCR90D!_nh_malloc_dbg_impl+0x0000001f
102cfadc MSVCR90D!_nh_malloc_dbg+0x0000002c
102db25b MSVCR90D!malloc+0x0000001b
102bd691 MSVCR90D!operator new+0x00000011
102bd71f MSVCR90D!operator new[]+0x0000000f
4113d8 Test2!AllocateMemory+0x00000028
41145c Test2!wmain+0x0000002c
411a08 Test2!__tmainCRTStartup+0x000001a8
41184f Test2!wmainCRTStartup+0x0000000f
7c816fd7 kernel32!BaseProcessStart+0x00000023
以粗体标记的行显示了我们代码中的函数。
注意:有时,可能会出现 “!heap -s” 命令不显示增长的堆的情况。在这种情况下,请使用 “!heap -stat -h” 命令列出所有堆及其大小和块数。找到增长的块数,然后使用 “!heap –flt s SIZE” (SIZE = 可疑块的大小) 命令。