请问内存泄漏检测的问题!

djsaflkdsj 2004-12-10 01:31:43
看MSDN里的"crtdbg.h file"说加下面三个语句:
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
在调试状态下,编译器就会输出信息显示是否有内存泄露.
我建了个文件"memoryleakdetect.c"如下:
#define CRTDBG_MAP_ALLOC
//#define _DEBUG
#include<stdlib.h>
#include<crtdbg.h>
#include<malloc.h>

void main()
{
//_CrtDumpMemoryLeaks();

int*pi=(int*)malloc(5*sizeof(int));
}
可怎么也弄不出来呀?!!!
...全文
219 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcahuang 2004-12-16
  • 打赏
  • 举报
回复
_CrtDumpMemoryLeaks()要放在程序的末尾,return之前。
参见:msdn文档

Visual Studio

Enabling Memory Leak Detection
The primary tools for detecting memory leaks are the debugger and the CRT debug heap functions. To enable the debug heap functions, include the following statements in your program:

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Note The #include statements must be in the order shown here. If you change the order, the functions you will use may not work properly.
By including crtdbg.h, you map the malloc and free functions to their Debug versions, _malloc_dbg and _free_dbg, which keep track of memory allocation and deallocation. This mapping occurs only in a debug build (in which _DEBUG is defined). Release builds use the ordinary malloc and free functions.

The #define statement maps the base versions of the CRT heap functions to the corresponding Debug versions. You do not absolutely need this statement, but without it, the memory leak dump will contain less useful information.

Once you have added the statements shown above, you can dump memory leak information by including the following statement in your program:

_CrtDumpMemoryLeaks();
When you run your program under the debugger, _CrtDumpMemoryLeaks displays memory leak information in the Output window. The memory leak information looks like this:

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.
If you do not use the #define _CRTDBG_MAP_ALLOC statement, the memory leak dump would look like this:

Detected memory leaks!
Dumping objects ->
{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.
Without _CRTDBG_MAP_ALLOC defined, the display shows:

The memory allocation number (inside the curly braces).
The block type (normal, client, or CRT).
The memory location in hexadecimal form.
The size of the block in bytes.
The contents of the first 16 bytes (also in hexadecimal).
With _CRTDBG_MAP_ALLOC defined, the display also shows you the file where the leaked memory was allocated. The number in parentheses following the filename (20, in this example) is the line number within the file.

To go to the line in the source file where the memory is allocated

Double-click on the line in the Output window that contains the filename and line number.
-or-

Select the line in the Output window that contains the filename and line number and press F4.
_CrtSetDbgFlag
Calling _CrtDumpMemoryLeaks is easy enough if your program always exits in the same place, but what if your program can exit from multiple locations? Instead of putting a call to _CrtDumpMemoryLeaks at each possible exit, you can include the following call at the beginning of your program:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
This statement automatically calls _CrtDumpMemoryLeaks when your program exits. You must set both bit fields, _CRTDBG_ALLOC_MEM_DF and _CRTDBG_LEAK_CHECK_DF, as shown above.

Setting the CRT Report Mode
By default, _CrtDumpMemoryLeaks dumps memory leak information to the Debug pane of the Output window, as described above. You can reset this to dump to another location using _CrtSetReportMode. If you use a library, it may reset the output to another location. In that case, you can set the output location back to the Output window using the following statement:

_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
For information on using _CrtSetReportMode to send output to other locations, see _CrtSetReportMode.


sharkhuang 2004-12-16
  • 打赏
  • 举报
回复
purify当然有window版本了啊
yingwang294 2004-12-16
  • 打赏
  • 举报
回复
没用过
icecools 2004-12-16
  • 打赏
  • 举报
回复
Vc里用Boundschecker不是很好吗
purify有windows版本的吗,俺们公司在Solaris上用的就是Purify
djsaflkdsj 2004-12-16
  • 打赏
  • 举报
回复
谢谢了
zhoumg 2004-12-16
  • 打赏
  • 举报
回复
Study!
NoneSoVile 2004-12-10
  • 打赏
  • 举报
回复
_CrtDumpMemoryLeaks()要放在程序的末尾
郑大满 2004-12-10
  • 打赏
  • 举报
回复
偶孤陋寡闻,窃问purify是什么东东?
djsaflkdsj 2004-12-10
  • 打赏
  • 举报
回复
可我这根本就没看到有内存泄漏的信息!!!
great_chenliang 2004-12-10
  • 打赏
  • 举报
回复
VC?应该可以在output里看到的。一般默认的debug模式下都会将未释放的内存地址给出的。
yjh1982 2004-12-10
  • 打赏
  • 举报
回复
没听说这种功能.
sharkhuang 2004-12-10
  • 打赏
  • 举报
回复
偶都是用purify

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧