<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; _CrtDumpMemoryLeaks()</title>
	<atom:link href="http://weseetips.com/tag/_crtdumpmemoryleaks/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to detect Memory Leaks by using CRT?</title>
		<link>http://weseetips.com/2008/06/17/how-to-detect-memory-leaks-by-using-crt/</link>
		<comments>http://weseetips.com/2008/06/17/how-to-detect-memory-leaks-by-using-crt/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 18:50:28 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Debugging Tips]]></category>
		<category><![CDATA[CRT memory leaks]]></category>
		<category><![CDATA[crtdbg.h]]></category>
		<category><![CDATA[detect memory leaks]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[_CRTDBG_MAP_ALLOC]]></category>
		<category><![CDATA[_CrtDumpMemoryLeaks()]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=178</guid>
		<description><![CDATA[Memory leaks are every programmers nightmare. Usually we used to use memory leak detection tools such as Numega BoundsChecker, Rational Purify to find memory leaks. But do you know that our C Runtime library have pretty good support for isolating memory leaks? You&#8217;ve to enable memory leak tracking by defining constant &#8211; _CRTDBG_MAP_ALLOC and then [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
<span style="color:#0000ff;"> Memory leaks </span>are every <span style="color:#0000ff;">programmers nightmare</span>. Usually we used to use <span style="color:#0000ff;">memory leak detection tools</span> such as <span style="color:#0000ff;">Numega BoundsChecker</span>, <span style="color:#0000ff;">Rational Purify</span> to find memory leaks. But do you know that our <span style="color:#0000ff;">C Runtime library have pretty good support for isolating memory leaks?</span></p>
<p><img class="alignnone size-medium wp-image-12" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /></p>
<p>You&#8217;ve to enable memory leak tracking by defining constant &#8211; <span style="color:#0000ff;">_CRTDBG_MAP_ALLOC</span> and then call &#8211; <span style="color:#0000ff;">_CrtDumpMemoryLeaks()</span>. Now the leaks will be dumped to the Visual Studio output window. See the following code snippet.</p>
<pre>// Declare this in header.
#define _CRTDBG_MAP_ALLOC
#include &lt;crtdbg.h&gt;
...
void DetectLeaks()
{
    // Create some leaks.
    CString* pString = new CString;
    BYTE* pBuffer = new BYTE[100];

    // Dump memory leaks to output window.
    _CrtDumpMemoryLeaks();
}</pre>
<p>When you call <span style="color:#0000ff;">_CrtDumpMemoryLeaks()</span> the memory leak information <span style="color:#0000ff;">will be dumped to the output window</span>. For instance, see the dump for memory leaks that we&#8217;ve made in <span style="color:#0000ff;">DetectLeaks()</span>.</p>
<pre>Detected memory leaks!
Dumping objects -&gt;
C:\Jijo\Studies\VC++\MFC\RabbitDlg\<span style="color:#0000ff;">RabbitDlgDlg.cpp(183)</span> : {194} normal block at 0x00294990, 100 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
C:\Jijo\Studies\VC++\MFC\RabbitDlg\<span style="color:#0000ff;">RabbitDlgDlg.cpp(182)</span> : {193} normal block at 0x00293B20, 4 bytes long.
 Data: &lt;   _&gt; 14 FB 8C 5F
...
Object dump complete.</pre>
<p>The filename and line number of leak will be present in the dump. So easy, nah?</p>
<p><img class="alignnone size-medium wp-image-18" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
While creating your MFC applications, did you ever noticed a code block as follows in your files?</p>
<pre>#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif</pre>
<p>This is for <span style="color:#0000ff;">debug information</span>. By using this information, the <span style="color:#0000ff;">_CrtDumpMemoryLeaks()</span> keep track of the filename. <span style="color:#0000ff;">If you comment out this, the filename will not be present in the dump</span> which will make our task difficult. So never remove those line. I swear, they are really important! <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-53" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/06/17/how-to-detect-memory-leaks-by-using-crt/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

