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

📄 memory_leaks.shtml

📁 mfc资源大全包含MFC编程各个方面的源码
💻 SHTML
字号:
<HTML>

<!-- Header information-->
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <!-- add your name in the CONTENT field below -->
   <META NAME="Author" CONTENT="Tom Moor">
   <TITLE><!-- Section Title -->Debugging - <!-- Article Title -->Finding memory leaks</TITLE>
</HEAD>

<!-- Set background properties -->
<body background="/fancyhome/back.gif" bgcolor="#FFFFFF">

<!-- A word from our sponsors... -->
<table WIDTH="100%">
<tr WIDTH="100%"><td align=center><!--#exec cgi="/cgi/ads.cgi"--><td></tr>
</table>



<CENTER><H3><FONT COLOR="#AOAO99">
<!-- Article Title -->Finding memory leaks
</FONT></H3></CENTER>
<HR>

<!-- Author and contact details -->
This article was contributed by <!-- Author Email --><A HREF="mailto:ilan@unforgettable.com"><!-- Author Name -->Ilan Kleinberger</A>.

<!-- Text / source code -->
<p><!-- The 'p' starts a paragraph of normal text -->
I recently came across something
that I found to be a major improvement in my ability to program in MS
Developer Studio version 5. It concerns memory leaks. 
When running the Debugger, if you have the Dump function on, you get a list
of objects that did not get deallocated. These are defined as 'Memory
Leaks'.

If we allocate memory for a character array in a function:

<!-- start a block of source code -->
<PRE><TT><FONT COLOR="#990000">
void MyFunction(void)
{
	char * MyCharArray = new char[10];
	.
	.
	.
	//delete [ ]MyCharArray;
}
<!-- end the block of source code -->
</FONT></TT></PRE>

but we don't free it, running the program will produce the message:

<!-- start a block of source code -->
<PRE><TT><FONT COLOR="#990000">
Detected memory leaks!
Dumping objects ->
C:\vcpp32\myproject\test.cpp(62) : {39} normal block at 0x00780DF0, 10 bytes
long.
 Data: <          > CD CD CD CD CD CD CD CD CD CD
Object dump complete.
<!-- end the block of source code -->
</FONT></TT></PRE>

<p>Now in this case, it gave us the CPP file and line number, but often it does
not. One way to find the leak would be to put a breakpoint in memory at the
memory address used by the allocation (assuming it's constant).

<p>But there's a better way!

<p>The number in the curly brackets, is an allocation number. Each allocation
receives a serial number. When you get a leak, you can use this number to
automatically place a breakpoint at the point in the program where the
allocation is being made. To do this, you can do one of two things.

<p>1. Add a call to the function _CrtSetBreakAlloc(), giving it the number in
the curly brackets.

<p>2. In the debugger, set the value of the variable _crtBreakAlloc to that
number.

<p>You want to do this as early in the program as possible. The effect of doing
that is a breakpoint on the line of the allocation of the 'new' function.
You then use the call stack to find the exact place in the program that
called the allocation.

(See MSDN article Q151585 for more information)
<!-- Posted / Update  date mm/dd/yy - add to the list -->
<p>Date Posted: <!-- date here -->17 May 1998



<P><HR>

<!-- Codeguru contact details -->
<TABLE BORDER=0 WIDTH="100%">
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; 1998 Zafir Anjum</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>

<!-- Counter -->


</BODY>
</HTML>



⌨️ 快捷键说明

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