【问题标题】:Visual Studio CrtDumpMemoryLeaks Broken?Visual Studio CrtDumpMemoryLeaks 坏了?
【发布时间】:2014-11-24 16:11:42
【问题描述】:

我似乎在使用 Visual Studios 内置的内存泄漏检测工具时遇到问题。 不管我做什么,它总是检测内存泄漏。

这里我有一个基本的 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 年也遇到过这种情况。

【问题讨论】:

  • 这看起来很奇怪,但没有设置报告模式。我很好奇如果你设置一个检查点是否会发生同样的事情,然后从检查点开始立即报告。 (我会自己检查一下,但我手边没有带 VS 的 Windows 框。

标签: c++ visual-studio memory-leaks


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多