内存泄漏在0行代码中

Memory leaks in 0 rows of code.

本文关键字:代码 0行 泄漏 内存      更新时间:2023-10-16

查看此最简单的应用程序:

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

它在调试窗格中产生下一个输出:

Detected memory leaks!
Dumping objects ->
{94} normal block at 0x012EA298, 8 bytes long.
 Data: <Dw      > 44 77 D9 00 00 00 00 00 
{93} normal block at 0x012F0DA8, 20 bytes long.
 Data: <  /   /   /     > A8 0D 2F 01 A8 0D 2F 01 A8 0D 2F 01 01 01 CD CD 
Object dump complete.

Detected memory leaks! 如何可能?
Win10,VS2015,目标平台版本10.0.14393.0

i"修复",然后使用下一个操作:

  1. stdlib在退出之前自行检查并显示内存泄漏
    _CRTSETDBGFLAG(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  2. 要查看我在exit.cpp:129中放置的消息
    exitProcess(return_code);

结果代码:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>  
#include <crtdbg.h>  
void main(void)
{
   // Show memory leaks before exit
   _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
   auto a = new int[33333];
   //_CrtDumpMemoryLeaks();  // DO NOT USE
}

现在我只能看到我的记忆泄漏在调试窗格中!

Detected memory leaks!
Dumping objects ->
{95} normal block at 0x014B4F48, 133332 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.

33333 * 4 = 133332。

感谢@jamesbean和MSDN文章"使用CRT库查找内存泄漏"