【发布时间】:2013-12-05 03:22:24
【问题描述】:
我正在尝试检测内存泄漏,并且我正在使用 make _CRTDBG_MAP_ALLOC 宏来定位泄漏区域的位置。所以我定义 MACRO 如下:
#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:Memory consumption increases quickly, then drops very slowly; memory leak?)
但输出显示如下:
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.
那么我怎样才能让它显示泄漏的文件名和位置呢?
【问题讨论】:
-
刚刚分配的代码没有使用您的#defines 编译。可以位于库中,也可以位于 DLL 中,例如预构建的 C++ 模板类特化。或者包含代码的 DLL 在生成泄漏报告之前被卸载。
-
@HansPassant 那么你是说不是我的“可疑代码”本身泄露了吗?是在“可疑代码”中调用了一些代码/bianraies/dll 吗?
-
我会说这是你没有找到的代码,可能在任何地方。泄漏的常见问题,不是吗?如果您可以让 {braces} 之间的数字重复,则将 _crtBreakAlloc 设置为该数字。
标签: c++ debugging memory-leaks crtdbg.h