⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teach_sp_33.htm

📁 VC专题教程 -- 第一章 Internet相关开发 打包下载 --- 1.1 如何编写CGI程序 ------ 1.2 一种更亲切的CGI开发系统WinCGI ------ 1.3 利用ISAPI开
💻 HTM
字号:

<!-- LANGUAGE='JavaScript'>write_body();<-->
<!-- LANGUAGE='JavaScript'>write_bar();<-->


<table width=98% cellspacing="0" cellpadding="0" align=center><!--整体框架-->
<tr><td>

<table border=0 width="100%" cellspacing="0" cellpadding="2"><!--标记放置区域-->
<tr>
	<td width="30%" align="center" bgcolor="#8E8E8E" valign=middle><img src=../../img/brand_200_60.gif width=200 height=60 alt="LOGO1"></td>
	<td width="70%" align="center" bgcolor="#8E8E8E" valign=middle><!-- LANGUAGE='JavaScript'>write_ban();<--></td>
</tr>
<tr>
	<td colspan="2" bgcolor="#939393" align=center><font color=white>您当前位置</font> <a href=../../index.htm><font color=white>首页</font></a> <a href=../index.htm><font color=white>开发教程</font></a> <a href=index.htm><font color=white><font class=engul>Visual C++/MFC</font>专题讲座</font></a> <font color=white>3.3 内存泄露检查</font> <font color=white><!-- LANGUAGE='JavaScript'>write_command();<--></font></td>
</tr>
</table><!--标记放置区域 END-->

<table border=0 width=100% cellspacing="0" cellpadding="0">
<tr bgcolor="#F4F4F4">
<td><!-- article title begin here-->
<br>
<p align=center><big>3.3 内存泄露检查</big></p>
	<table border=0 align=center width=100%>
	<tr><td><small>
<!-- article content begin here-->
<p>在VC中提供内存检查的机制是跟踪new操作,也就是说所有的new操作都会被记录,如果通过new操作所分配的内存未被正常delete将会在程序退出时在调试窗口中显示出具体的内存泄露信息。
<p>同样通过malloc分配的内存也会被跟踪,但是在显示时就不会知道实在程序中何处进行了malloc操作。先看一下下面的例子:
<pre>
void _tmain()
{
...
	char *pszNew=(char*)malloc(200);

	char *pszNew2=new char[100];
	CString *pszNew3=new CString("test");
...
}
//通过调试方式运行后并退出,可以看到调试信息中关于内存泄露的信息如下:
Detected memory leaks!
Dumping objects -&gt
strcore.cpp(118) : {37} normal block at 0x007702E0, 17 bytes long.
 Data: &lt            test&gt 01 00 00 00 04 00 00 00 04 00 00 00 74 65 73 74 
<font color=red>G:\temp2\sam_sp_33\sam_sp_33.cpp(42)</font> : {36} normal block at 0x00770520, 4 bytes long.
 Data: &lt  w &gt EC 02 77 00 
<font color=blue>//对于CString *pszNew3=new CString("test");产生的信息</font>
<font color=red>G:\temp2\sam_sp_33\sam_sp_33.cpp(41)</font> : {35} normal block at 0x00770320, 100 bytes long.
 Data: &lt                &gt CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
<font color=blue>//对于char *pszNew2=new char[100];产生的信息</font>
{34} normal block at 0x007703B0, 200 bytes long.
 Data: &lt                &gt CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
<font color=blue>//对于char *pszNew=(char*)malloc(200);产生的信息</font>
Object dump complete.
</pre>
<p>可以看到通过new分配的内存在显示信息时会报告出在那一个文件的那一行进行的new操作,而通过malloc分配的内存则仅仅是显示出内存泄露的信息而无法定位分配内存的程序位置。
<p>此外如果需要在文件头部定义DEBUG_NEW宏才可以正确的跟踪new操作。具体代码如下:
<pre>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
</pre>
<p>由于对new操作的跟踪只需要在调试版本中出现所以使用了条件编译。
<p>我们可以看到VC所提供的检查内存泄露的方式是非常易于使用的,我们在开发程序时一定要注意内存的分配问题,特别是对于一些长时间运行的程序。<a href=sam_sp_33.zip>下载本节代码</a>
<!-- article content end-->
<p align=center><a href=index.htm#charpter3>返回</a></p>
	</td></tr>
	</table>
<p align=center><small>版权所有 闻怡洋 <a href=http://www.vchelp.net/><font class=engul>http://www.vchelp.net/<font></a></small></p>
</td>
</tr><!-- article title end-->
</table>

</td></tr></table><!--整体框架 END-->




<!-- LANGUAGE='JavaScript'>write_tail();<-->


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -