使用CRT库检测Metro应用程序中的内存泄漏

时间:2013-07-23 08:13:14

标签: memory-leaks 3d windows-runtime microsoft-metro

我正在为Windows Store开发3D游戏。我在游戏中检测到了内存泄漏,但在调试时我无法看到输出中内存泄漏的文件名和行号。以下是我为检测内存泄漏而包含的代码行:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

_CrtDumpMemoryLeaks();

以下是调试应用程序的输出:

Detected memory leaks!
Dumping objects ->
{1686} normal block at 0x06FD72E8, 8 bytes long.
Data: <        > 08 F5 FE 03 00 00 00 00 
{1685} normal block at 0x03FEF500, 40 bytes long.
Data: <    x    r      > 20 E5 B4 01 78 EE FE 03 E8 72 FD 06 00 A9 03 04 
{1684} normal block at 0x0403A900, 64 bytes long.
Data: <W i n d o w s . > 57 00 69 00 6E 00 64 00 6F 00 77 00 73 00 2E 00 
{1676} normal block at 0x0406C858, 36 bytes long.
Data: <         K      > FF FF 00 00 FF EE 82 EE FF 4B 00 82 FF 00 00 FF 
{1658} normal block at 0x06FD7208, 8 bytes long.
Data: <        > 80 EE FE 03 00 00 00 00 
{1657} normal block at 0x03FEEE78, 40 bytes long.
Data: <         r   J  > 00 F5 FE 03 F8 F3 FE 03 08 72 FD 06 E0 4A F7 06

然而,根据Microsoft博客,当代码中包含 _CRTDBG_MAP_ALLOC 时,我应该获取文件名和行号:

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} 
normal block at 0x00780E80, 64 bytes long.
Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

即使在代码中包含 _CRTDBG_MAP_ALLOC 之后,我也没有得到类似的输出(文件名和行号)。请帮我解决这个问题。

提前致谢!

1 个答案:

答案 0 :(得分:2)

SO

上有类似的问题

这有用吗?

#ifdef _DEBUG
#define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW_PLACEMENT
#endif

int* p = new DEBUG_NEW_PLACEMENT int(5);

或者,如果内存分配编号(花括号内)始终相同,则可以使用数字设置断点,例如

_CrtSetBreakAlloc(18);

详情here