web-dev-qa-db-ja.com

_CRTDBG_MAP_ALLOCにファイル名が表示されない

メモリリークを検出しようとしています。make_CRTDBG_MAP_ALLOCマクロを使用して、リーク領域を特定しています。だから私は次のようにマクロを定義しています:

#ifdef _DEBUG
    #define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
#endif

私のコードには、次のものがあります。

UINT SomeFunThread( LPVOID pParam )
{
   _CrtMemState crtMemStateStart;
    _CrtMemState crtMemStateFinish;

    _CrtMemCheckpoint(&crtMemStateStart);


    // My suspisious code


     _CrtMemCheckpoint(&crtMemStateFinish);

      int nDifference(0);
      _CrtMemState crtMemStateDifference;
      nDifference = _CrtMemDifference(&crtMemStateDifference, &crtMemStateStart, &crtMemStateFinish);

    if(nDifference > 0)
        _CrtDumpMemoryLeaks();

    return 0;
}

(Tushar Jadhavのおかげで: メモリ消費量は急速に増加し、その後非常にゆっくりと減少します;メモリリーク?

しかし、出力は次のようになります。

Detected memory leaks!
Dumping objects ->
{124058} normal block at 0x0000000031DED080, 24 bytes long.
 Data: < 0      ` $     > C8 30 F7 EF FE 07 00 00 60 D2 24 1D 00 00 00 00 

このようなものの代わりに:

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} 
normal block at 0x00780E80, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

では、これにリークのファイル名と場所を表示させるにはどうすればよいですか?

20
Nick X Tsui

そのcppファイルでCRTがオンになっている場合にのみ、リークの行が表示されるようです。

7
Nick X Tsui

私の場合、 this スレッドの内容をコードに含めることになりました。これはnew演算子をオーバーライドし、後で印刷できるようにファイルの名前と行番号をファイルに含めます。これがVisualStudioにのみ適用できるかどうかはわかりません。

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

参照されているソースからのテストコード全体は次のとおりです。

#define _CRTDBG_MAP_ALLOC
#include<iostream>
#include <crtdbg.h>
#ifdef _DEBUG
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
#endif

int main() 
{
    char *a = new char[10];
    _CrtDumpMemoryLeaks();
    return 0; 
}

私のテストケースではこれが印刷されます:

Detected memory leaks!
Dumping objects ->
e:\test\testapplication\testapplication.cpp(11) : {144} normal block at 0x007F4EF0, 10 bytes long.
 Data: <          > CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
11
Krøllebølle