用windbg 检查内存泄漏
1.下载编译https://github.com/0cch/luadbg
2.编写脚本1.txt
.load luadbg_v15
*.sympath+ srv*c:\MyServerSymbols*https://msdl.microsoft.com/download/symbols
x *!*CrtDumpMemoryLeaks*
bm *!*CrtDumpMemoryLeaks* ".logopen temp.txt;gu;.logclose;!lua analyse_leak.lua"
bl
gc
3.编写脚本analyse_leak.lua
function Sleep(n) if n > 0 then os.execute("ping -n " .. tonumber(n + 1) .. " localhost > NUL") end end print('---1\n') local open = io.open local function read_file(path) local file = open(path, "rb") -- r read mode and b binary mode if not file then return nil end local content = file:read "*a" -- *a or *all reads the whole file file:close() return content end local function exec_from_io(cmd) local path='temp.txt' exec(string.format('.logopen %s', path)) exec(cmd) Sleep(1) exec('.logclose') local file = open(path, "rb") -- r read mode and b binary mode if not file then return nil end local content = file:read "*a" -- *a or *all reads the whole file file:close() return content end print('---2\n') local leak_str = read_file('temp.txt') --print('---leak_str:', leak_str, '\n') --g:\temp\mfcapplication2\mfcapplication2\mfcapplication2dlg.cpp(74) : {286} normal block at 0x000CD920, 4 bytes long. print('---3\n') pe=1 i=1 while (true) do print('-----------------',i,'\n') i = i+1 p2b, p2e, addr= string.find(leak_str, 'normal block at ([^,]*),', pe) print('---p2b, p2e, addr',p2b, p2e, addr, '\n') if p2b==nil then break end cmd=string.format('!heap -p -a %s', addr) print('---cmd:',cmd,'\n') stacks=exec_tostring(cmd) print('---stacks\n', stacks) pe = p2e end
4. attach程序,执行 $<1.txt
5.程序运行,执行令他泄露,然后退出
6.程序会break到windbg里面,显示出泄露信息,比如在我这边的输出
0:000> !lua analyse_leak.lua
---1
---2
---3
----------------- 1
---p2b, p2e, addr 148 174 0x088C4FF8
---cmd: !heap -p -a 0x088C4FF8
---stacks
address 088c4ff8 found in
_DPH_HEAP_ROOT @ 57c1000
in busy allocation ( DPH_HEAP_BLOCK: UserAddr UserSize - VirtAddr VirtSize)
8852c64: 88c4fd8 28 - 88c4000 2000
5360a65c verifier!AVrfDebugPageHeapAllocate+0x0000023c
778768c5 ntdll!RtlDebugAllocateHeap+0x0000003c
777d553c ntdll!RtlpAllocateHeap+0x000017dc
777d2f8a ntdll!RtlpAllocateHeapInternal+0x0000017a
777d2dfe ntdll!RtlAllocateHeap+0x0000003e
06225028 ucrtbased!heap_alloc_dbg_internal+0x00000198
06224e36 ucrtbased!heap_alloc_dbg+0x00000036
0622760a ucrtbased!_malloc_dbg+0x0000001a
0fb91257 mfc140ud!operator new+0x00000037
0fb912e6 mfc140ud!operator new+0x00000016
00d785aa MFCApplication2+0x000185aa
0fe5f48d mfc140ud!AfxDlgProc+0x0000005d
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a3ac48 USER32!UserCallDlgProcCheckWow+0x000002a8
74a3a307 USER32!DefDlgProcWorker+0x000000c7
74a3a224 USER32!DefDlgProcW+0x00000054
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a55913 USER32!UserCallWinProcCheckWow+0x000002d3
74a4b387 USER32!CallWindowProcW+0x00000097
0ff7fb7a mfc140ud!CWnd::DefWindowProcW+0x0000004a
0ff7fc6f mfc140ud!CWnd::Default+0x0000005f
0fe609b7 mfc140ud!CDialog::HandleInitDialog+0x00000137
0ff86d1d mfc140ud!CWnd::OnWndMsg+0x00000e2d
0ff8abb6 mfc140ud!CWnd::WindowProc+0x00000056
0ff7c1b8 mfc140ud!AfxCallWndProc+0x00000128
0ff7d1b5 mfc140ud!AfxWndProc+0x000000b5
0fcca0ad mfc140ud!AfxWndProcBase+0x0000006d
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a55913 USER32!UserCallWinProcCheckWow+0x000002d3
74a413da USER32!SendMessageWorker+0x0000028a
74a3ebef USER32!InternalCreateDialog+0x000011df
74a4f075 USER32!CreateDialogIndirectParamAorW+0x00000035
----------------- 2
---p2b, p2e, addr 296 322 0x088C4FF9
---cmd: !heap -p -a 0x088C4FF9
---stacks
address 088c4ff9 found in
_DPH_HEAP_ROOT @ 57c1000
in busy allocation ( DPH_HEAP_BLOCK: UserAddr UserSize - VirtAddr VirtSize)
8852c64: 88c4fd8 28 - 88c4000 2000
5360a65c verifier!AVrfDebugPageHeapAllocate+0x0000023c
778768c5 ntdll!RtlDebugAllocateHeap+0x0000003c
777d553c ntdll!RtlpAllocateHeap+0x000017dc
777d2f8a ntdll!RtlpAllocateHeapInternal+0x0000017a
777d2dfe ntdll!RtlAllocateHeap+0x0000003e
06225028 ucrtbased!heap_alloc_dbg_internal+0x00000198
06224e36 ucrtbased!heap_alloc_dbg+0x00000036
0622760a ucrtbased!_malloc_dbg+0x0000001a
0fb91257 mfc140ud!operator new+0x00000037
0fb912e6 mfc140ud!operator new+0x00000016
00d785aa MFCApplication2+0x000185aa
0fe5f48d mfc140ud!AfxDlgProc+0x0000005d
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a3ac48 USER32!UserCallDlgProcCheckWow+0x000002a8
74a3a307 USER32!DefDlgProcWorker+0x000000c7
74a3a224 USER32!DefDlgProcW+0x00000054
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a55913 USER32!UserCallWinProcCheckWow+0x000002d3
74a4b387 USER32!CallWindowProcW+0x00000097
0ff7fb7a mfc140ud!CWnd::DefWindowProcW+0x0000004a
0ff7fc6f mfc140ud!CWnd::Default+0x0000005f
0fe609b7 mfc140ud!CDialog::HandleInitDialog+0x00000137
0ff86d1d mfc140ud!CWnd::OnWndMsg+0x00000e2d
0ff8abb6 mfc140ud!CWnd::WindowProc+0x00000056
0ff7c1b8 mfc140ud!AfxCallWndProc+0x00000128
0ff7d1b5 mfc140ud!AfxWndProc+0x000000b5
0fcca0ad mfc140ud!AfxWndProcBase+0x0000006d
74a634bb USER32!_InternalCallWinProc+0x0000002b
74a55913 USER32!UserCallWinProcCheckWow+0x000002d3
74a413da USER32!SendMessageWorker+0x0000028a
74a3ebef USER32!InternalCreateDialog+0x000011df
74a4f075 USER32!CreateDialogIndirectParamAorW+0x00000035
----------------- 3
---p2b, p2e, addr <out of memory> <out of memory> <out of memory>