WEB开发网
开发学院软件开发VC VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏... 阅读

VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏

 2007-03-17 21:27:37 来源:WEB开发网   
核心提示: #define _CRTDBG_MAP_ALLOC#include<stdlib.h>#include<crtdbg.h>注意:#include 语句的顺序,如果更改此顺序,VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏(3),所使用的函数可能无法正确
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>

注意:#include 语句的顺序。如果更改此顺序,所使用的函数可能无法正确工作。

通过包含 crtdbg.h 头文件,可以将 malloc 和 free 函数映射到其“调试”版本 _malloc_dbg 和 _free_dbg,这些函数会跟踪内存分配和释放。此映射只在调试(Debug)版本(也就是要定义 _DEBUG)中有效。发行版本(Release)使用普通的 malloc 和 free 函数。

#define 语句将 CRT 堆函数的基础版本映射到对应的“调试”版本。该语句不是必须的,但如果没有该语句,那么有关内存泄漏的信息会不全。

二是在需要检测内存泄漏的地方添加下面这条语句来输出内存泄漏信息:

_CrtDumpMemoryLeaks();  当在调试器下运行程序时,_CrtDumpMemoryLeaks 将在 Output 窗口的 Debug 页中显示内存泄漏信息。比如:Detected memory leaks!
Dumping objects ->
C:Tempmemleakmemleak.cpp(15) : {45} normal block at 0x00441BA0, 2 bytes long.
Data: <AB> 41 42
c:program filesmicrosoft visual studiovc98includecrtdbg.h(552) : {44} normal block at 0x00441BD0, 33 bytes long.
Data: < C > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
c:program filesmicrosoft visual studiovc98includecrtdbg.h(552) : {43} normal block at 0x00441C20, 40 bytes long.
Data: < C > 08 02 43 00 16 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.

如果不使用 #define _CRTDBG_MAP_ALLOC 语句,内存泄漏的输出是这样的:

上一页  1 2 3 4 5 6 7 8  下一页

Tags:VC 如何 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接