Visual Studio CrtDumpMemoryLeaks Broken?

Visual Studio CrtDumpMemoryLeaks Broken?

本文关键字:Broken CrtDumpMemoryLeaks Studio Visual      更新时间:2023-10-16

我似乎在使用Visual Studio内置内存泄漏检测工具时遇到了问题。无论我做什么,它总是检测到内存泄漏。

这里我有一个基本的C++主,它启用了内存泄漏检测(根据MSDN)。

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
int main (){
    _CrtDumpMemoryLeaks ();
    return 0;
}

由于未知原因,它表示内存泄漏。

Detected memory leaks!
Dumping objects ->
{142} normal block at 0x0000005934F90660, 16 bytes long.
Data: < 3"             > C8 33 22 DC F6 7F 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

其他人经历过这种情况吗?有人知道是什么原因造成的吗?

p.S我正在使用Visual Studio 2013,但我在2012年和2010年也经历过这种情况。

"_CrtDumpMemoryLeaks();"将显示执行时的内存状态,即您刚刚分配了一个内存块。

int main (){
    std::string tmp("Hello");
    _CrtDumpMemoryLeaks ();  //this will show you have a memory leak (in the string object)
    return 0;
}

使用

int main (){
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    std::string tmp("Hello");
    return 0;
}

程序退出后,您将在"输出"窗口中找到调试输出。由于系统已删除字符串对象,因此没有内存泄漏。

如果您还包括

#ifdef _DEBUG
#define new new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )  
#endif

首先,在所有代码文件中(或者首先是一个common.h文件),您将获得分配有问题内存的位置。

如果您重新定义了新的运算符,这将不起作用。