平台:Windows7 64bit,編譯器G++(mingw)
工具:Dr Memory,項目主頁:https://code.google.com/p/drmemory/ (可能要翻牆,可能會很慢,所以,可以直接按照下面官方主頁給出的鏈接下載,我也放了一份Windows版的在百度網盤,http://pan.baidu.com/s/1qWv2tZm,這個應該肯定可以下載)
The latest versions can be directly accessed here:
2014-08-30日摘錄於https://code.google.com/p/drmemory/wiki/Downloads?tm=2
用法類似於Linux下的Valgrind,非常簡單
測試代碼:
#include <cstdio> int main(int argc, const char *argv[]) { char *s; s = new char[100]; return 0; }
用g++編譯,g++ test.cpp -o test (貌似必須有-o選項指定輸出文件名,用g++ test.cpp會生成a.exe,但是用drmemory會導致顯示"a.exe停止工作")
執行drmemory test.exe
這樣應該就會生成一個報告,可以看到Error #1: LEAK 100 direct bytes 0x00c80ea8-0x00c80f0c + 0 indirect bytes
關於微軟的App Verifier,http://msdn.microsoft.com/en-us/library/windows/hardware/ff551063(v=vs.85).aspx, 按照說明可以下載其中的debugging tool,安裝成功后,搞了半天也沒學會怎么用……
我是用mingw的工具編譯和調試,g++ file.cpp -o file,生成exe,打開App Verifier,添加這個exe,執行gdb file.exe,run的時候確實看到說App Verifier enable了,但是無論是運行過程中還是運行之后都看不到log。很可能是只能和windbg工具一起使用,可能還要用微軟的編譯器。搜索 “App Verifier 內存泄露”可以得到很多相關文章,比如這篇文章:http://www.zzfjh.com/?p=105
參考:
https://code.google.com/p/drmemory/
http://latedev.wordpress.com/2012/05/19/valgrind-for-windows/