c++ - 在一个非常简单的程序中检测到内存泄漏。该怎么办?

标签 c++ visual-studio-2012 memory-leaks msvcrt

Visual Studio CRT 调试系统检测到我的大程序内存泄漏。我将我的程序缩减为以下内容,但仍然显示内存泄漏。

#include "stdafx.h"
#include "crtdbg.h"

int main()
{
    int tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
    _CrtSetDbgFlag(tmp | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
    int* k = new int(8);
    delete k;
    return 0;
}

当我在我的 Visual Studio 2012 系统中运行它时,我看到以下内容:

Detected memory leaks!
Dumping objects ->
{65} normal block at 0x00663008, 4424 bytes long.
 Data: <X    #f         > 58 CF 14 00 90 23 66 00 01 00 00 00 00 00 00 00 
{64} normal block at 0x00662390, 4 bytes long.
 Data: <    > 00 C3 14 00 
Object dump complete.

如果我删除分配和释放,泄漏就不会出现。如果我用任何使用内存分配的标准库功能(例如 std::string k)替换分配和释放,就会出现泄漏。

为什么会出现内存泄漏?我怎样才能删除它们?


我尝试通过将 _crtBreakAlloc 设置为 64 来调试我的问题;系统停在一个应该帮助我的地方(见下面的堆栈跟踪)。但我不知道如何处理这些信息。

>   test_it.exe!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 393   C++
    test_it.exe!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239    C++
    test_it.exe!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302  C++
    test_it.exe!malloc(unsigned int nSize) Line 56  C++
    test_it.exe!_PlatformSpecificMalloc()  Unknown
    test_it.exe!MemoryLeakWarningPlugin::ignoreAllLeaksInTest(void) Unknown
    test_it.exe!operator new(unsigned int)  Unknown
    test_it.exe!MemoryLeakWarningPlugin::getGlobalDetector(void)    Unknown
    test_it.exe!std::error_condition::value(void)   Unknown
    test_it.exe!operator new(unsigned int)  Unknown
    test_it.exe!main() Line 9   C++

我的系统是:

  • Microsoft Visual Studio 专业版 2012
  • 版本 11.0.61030.00 更新 4
  • Visual C++ 2012 04938-004-0034007-02224
  • Windows 7

最佳答案

您的调用堆栈表明,除了 Visual C++ 运行时函数之外,还使用了另一个内存泄漏工具。

使用谷歌将我带到这个链接:https://github.com/auser/cpputest/blob/master/src/CppUTest/MemoryLeakWarningPlugin.cpp

很可能,cppuTest 已在您不知情的情况下应用于您的简单项目。

我建议您创建一个全新的 Win32 控制台应用程序,复制并粘贴您的代码,然后重新测试。确保新项目没有额外的依赖项。

关于c++ - 在一个非常简单的程序中检测到内存泄漏。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28211281/

相关文章:

visual-studio-2012 - nvcc.exe 链接错误 Microsoft Visual Studio 配置文件 'vcvars64.bat' 找不到

c# - 使用 TFS 2012 API,如何获取用户的电子邮件地址?

python - 垃圾收集 python rss 内存

c++ - 如何在不发生冲突的情况下快速散列非常大的子串?

c++ - 对象组合促进代码重用。 (T/F,为什么)

C++ 多重继承三角形

c++ - 哪个版本的 Dinkumware STL Lib 随 Visual Studio 2012 一起提供?

iphone - 弹出 Controller 时 UINavigationController 内存不会减少

c++ - 调试断言失败…_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

c++ - 成员变量中的类模板参数推导