2014-09-30 57 views
1

我似乎在Visual Studio内置内存泄漏检测工具中遇到问题。 无论我做什么,它总是检测到内存泄漏。Visual Studio CrtDumpMemoryLeaks破碎?

这里我有一个基本的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年

+0

这似乎很奇怪,但不设置报告模式。我很好奇,如果你设置了一个检查点,同样的事情发生,那么从检查点开始立即报告。 (我会自己检查一下,但我没有一个方便使用Windows的VS盒子。 – WhozCraig 2014-09-30 06:25:43

回答

0

的 “_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 

先在你的所有代码文件(或第i个共同的.h文件中),你将获得其中的违规分配内存的地方。

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