Visual StudioでC++を使ってコードを書いているとき、メモリリークを見つける便利なデバッグ機能がある。
Download Visual Studio 2005 Retired documentation from Official Microsoft Download Center
プログラムの終了時に_CrtDumpMemoryLeaks関数を呼べば、メモリリークをダンプしてくれる。
#include "stdafx.h" #include <crtdbg.h> int _tmain(int argc, _TCHAR* argv[]) { int* p = new int(); _CrtDumpMemoryLeaks(); return 0; }
例えばこのコードをデバッグ有りでビルドし実行した場合、次のような結果がデバッグコンソールに表示される。
Detected memory leaks! Dumping objects -> {112} normal block at 0x00038020, 4 bytes long. Data: < > 00 00 00 00 Object dump complete.
リリースビルドした場合には_CrtDumpMemoryLeaks関数は実行されない。