概要 †
Windowsであれば、Microsoftのcrtdbg機能が使えます。
MFCを使っている場合は、デフォルトでDebugビルド時に一部のメモリリークを検出できます。
しかし、検出できる項目が足りないですし、デバッガーの出力ウィンドウにしか表示されないので物足りません。
以下の様なコードをプロジェクト毎に埋め込むと、メモリリーク検出処理がカスタマイズできます。
ただし、crtdbgでは動的配列外アクセス(読み込み)は検出出来ないので、RobustArrayNewと組み合わせるとより効果的です。
crtdbgの動作サンプルも、RobustArrayNewにあります。
ソース †
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| -
|
!
-
-
!
|
|
|
|
|
|
-
|
|
|
|
|
|
!
|
|
|
-
|
!
| #ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
int _tmain(int , _TCHAR* )
{
#ifdef _DEBUG
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
]
|
検証時の環境 †
- Windows 8.1 Pro x64
- Windows 7 SP1 x86
- Windows Vista Ultimate SP2(x86)
- Visual Studio 2008 Standard Edition