<?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>DEV &#187; OS Development</title>
	<atom:link href="http://thegothicparty.com/dev/category/macos/feed/" rel="self" type="application/rss+xml" />
	<link>http://thegothicparty.com/dev</link>
	<description>Developement notes</description>
	<lastBuildDate>Mon, 31 Oct 2011 06:55:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Finding hidden memory leak with Instruments</title>
		<link>http://thegothicparty.com/dev/article/finding-hidden-memory-leak-with-instruments/</link>
		<comments>http://thegothicparty.com/dev/article/finding-hidden-memory-leak-with-instruments/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 07:20:52 +0000</pubDate>
		<dc:creator>dev</dc:creator>
				<category><![CDATA[OS Development]]></category>
		<category><![CDATA[Tech Articles]]></category>

		<guid isPermaLink="false">http://thegothicparty.com/dev/?p=275</guid>
		<description><![CDATA[Using the 1 Mb NSDateFormatter memory leak as a case-study, this tutorial shows how to locate and isolate unexpected memory allocation using Instruments. Technically, this 1 Mb loss is arguably not a leak, since the data is cached, and is still actively referenced by the system. Yet&#8230; Instruments has built-in tools to isolate true leaks [...]]]></description>
			<content:encoded><![CDATA[<p>Using the 1 Mb <a title=" NSDateFormatter memory leak" href="http://thegothicparty.com/dev/iphone/nsdateformatter-memory-leak/">NSDateFormatter memory leak</a> as a case-study, this tutorial shows how to locate and isolate  unexpected memory allocation using Instruments. Technically, this 1 Mb loss is arguably not a leak, since the data is cached, and is still actively referenced by the system. Yet&#8230;</p>
<p><a href="http://thegothicparty.com/dev/macos/finding-hidden-memory-leak-with-instruments/"><img class="size-full wp-image-276      alignnone" style="border: 0pt none;" title="Finding hidden memory leak with Instruments" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss00.png" alt="" width="256" height="194" /></a><em>Instruments has built-in tools to isolate true leaks (lost blocks) but can also be used to locate memory loss.</em><em> While this session references iPhone OS 3.1.3, it can be generalized: only the  example is specific, not the concept.</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><span id="more-275"></span></p>
<p>&#8230;as an engineer, I tend to differ: if every time I invoked a library API, I&#8217;d loose roughly 1 megabyte of RAM, I&#8217;d soon be running out of memory. I will also argue that, since this memory &#8220;cache&#8221; is neither documented nor explicitly reported it is, for all purposes, a leak(*).</p>
<p><span style="color: #1459ea;">(*) A leak is a lost block. But not all lost blocks qualify as leaks. This article focuses on the ones that don&#8217;t. Just like leaks, these lost blocks are consuming memory space. That space is generally not reclaimed until the host application quits.</span></p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>1. Launch Instruments</h2>
<p style="text-align: center;">From XCode, launch your application using &#8220;Object Allocations&#8221;<br />
<a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss01.png"><img class="size-full wp-image-277 aligncenter" style="border: 0pt none;" title="XCode &gt; Run &gt; Start with Performance Tool &gt; Object Allocations" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss01.png" alt="" width="512" height="324" /></a>For this tutorial, we are not using Leaks Instrument Template. While Leaks is very effective against true leaks, it is oblivious to retained but forgotten objects, over-retained objects, or poor API implementation.</p>
<div style="clear: both;"><!-- reset --></div>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>2. Run your application<!--more--></h2>
<p>In this example, only 32 seconds (about 0.54 minutes in Instruments notation) were needed to expose the unexpected memory usage.<br />
<a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss02.png"><img class="alignnone size-full wp-image-309" style="border: 0pt none;" title="Run your application" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss02.png" alt="" width="669" height="464" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p>It is a good idea to be generous in early scoping of memory usage, since it will fluctuate. For the purpose of this tutorial, this preliminary analysis phase has been skipped entirely.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>3. Stop the Application execution</h2>
<p>It has been established during other sessions that the particular memory consumption we are studying does not change during the lifetime of this application. Stopping it after 34 seconds.</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss03.png"><img class="alignnone size-full wp-image-311" style="border: 0pt none;" title="Stop" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss03.png" alt="" width="103" height="52" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p>Instruments can start/stop/save/restore sessions, which makes it invaluable to compare the program behavior before/after alterations.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>4. Objects Created and Still Living</h2>
<p>Focus on the objects that have been allocated, but not de-allocated.</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss04.png"><img class="alignnone size-full wp-image-312" style="border: 0pt none;" title="Objects Created &amp; Still Living" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss04.png" alt="" width="215" height="68" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p>Obviously, objects created but no longer living have been de-allocated, and are not of particular interest while searching for leaks.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>5. Observe the Object Allocation Graph</h2>
<p>Looking at the graph, it is easy to spot sudden memory allocation. For increased granularity, reduce the &#8220;Inspection Range&#8221;.<br />
<a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss05.png"><img class="alignnone size-full wp-image-313" style="border: 0pt none;" title="Inspection Range" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss05.png" alt="" width="544" height="158" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p>The Inspection Range will reduce the number of items present in the Diagram View below.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>6. Set the View mode to Diagram View</h2>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss06.png"><img class="alignnone size-full wp-image-314" style="border: 0pt none;" title="View Mode" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss06.png" alt="" width="237" height="28" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>7. Navigate the Diagram View</h2>
<p>This is a very handy feature of Instruments. By sliding or dragging the time line cursor, the Diagram View selection will scroll accordingly. By bringing the triangular cursor precisely over the edge of the sharp raise of memory usage around the 0.54 min (32.5 seconds) area, one discovers pages and pages of a repeated call to -[NSDateFormatter getObjectValue:forString:range:error:]</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss07.png"><img class="alignnone size-full wp-image-315" style="border: 0pt none;" title="Navigate" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss07.png" alt="" width="544" height="158" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>8. Focus on the Diagram View</h2>
<p>Observing the Diagram View, sorted either by &#8220;Responsible Caller&#8221; or by &#8220;Creation Time&#8221;, one can see the repeated invocation to the same NSDateFormatter method.</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss08.png"><img class="alignnone size-full wp-image-318" style="border: 0pt none;" title="Focus" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss08.png" alt="" width="669" height="461" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>9. Call Stack</h2>
<p>Turn on the call stack view (Extended Detail View)</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss09.png"><img class="alignnone size-full wp-image-319" style="border: 0pt none;" title="Extended Detail View" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss09.png" alt="" width="237" height="28" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>10. Final analysis</h2>
<p>By selecting a single line in the Diagram View, and inspecting the corresponding call stack in the Extended Detail View, one can focus on a single invocation and, hopefully, isolate the code responsible for the memory allocation. A double-click will jump to the source code, if available.</p>
<p>For example, +[Manager dateFromString] is the method responsible for invoking the suspicious NSDateFormatter -dateFromString selector.</p>
<p><a href="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss10.png"><img class="alignnone size-full wp-image-320" style="border: 0pt none;" title="Call Stack" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/ss10.png" alt="" width="671" height="454" /></a></p>
<div style="clear: both;"><!-- reset --></div>
<p>Two facts will attract our attention:</p>
<ol>
<li>There are pages upon pages of NSDateFormatter invocations, all grouped together when sorted by Responsible Caller.</li>
<li>When sorted by Creation Time, about 8000 invocations to NSDateFormatter take place within the same 100 ms.</li>
</ol>
<p>When looked at closely, this study lead to a decision to not use NSDateFormatter time zones, which turned out to be the culprit.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<h2>11. Related links</h2>
<ul>
<li><a href="http://thegothicparty.com/dev/iphone/nsdateformatter-memory-leak/">NSFormatter memory leak</a></li>
<li><a href="http://en.wikipedia.org/wiki/Memory_leak">Wikipedia article about memory leak</a></li>
</ul>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="3" /><br />
</em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<p><script src="http://widgets.digg.com/buttons.js" type="text/javascript"></script></p>
<p><a onclick="window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=thegothicparty.com%2Fdev%2Fmacos%2Ffinding-hidden-memory-leak-with-instruments&amp;title=Finding+hidden+memory+leak+with+Instruments', 'delicious', 'toolbar=no,width=550,height=550'); return false;" href="http://delicious.com/save"><img style="border: 0pt none;" src="http://www.delicious.com/static/img/delicious.small.gif" alt="Delicious" width="16" height="16" />Bookmark this on Delicious</a></p>
<p><a class="DiggThisButton DiggCompact" rev="news, programming" href="http://digg.com/submit?url=http%3A%2F%2Fthegothicparty.com%2Fdev%2Fmacos%2Ffinding-hidden-memory-leak-with-instruments&amp;title=Finding+hidden+memory+leak+with+Instruments"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://thegothicparty.com/dev/article/finding-hidden-memory-leak-with-instruments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSDateFormatter memory leak</title>
		<link>http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/</link>
		<comments>http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 20:15:08 +0000</pubDate>
		<dc:creator>dev</dc:creator>
				<category><![CDATA[OS Development]]></category>
		<category><![CDATA[Tech Articles]]></category>

		<guid isPermaLink="false">http://thegothicparty.com/dev/?p=231</guid>
		<description><![CDATA[This is a follow-up to a post on stackoverflow.com, where it appears that 1 MB is lost after invoking NSFormatter -dateFromString. I ran into the same situation, with very similar leak sizes to the ones reported on that article. Instruments shows that 868 Kb (889520 bytes to be exact) are allocated by Cocoa on iPhone [...]]]></description>
			<content:encoded><![CDATA[<p>This is a follow-up to a post on <a title="Instruments (Leaks) and NSDateFormatter" href="http://stackoverflow.com/questions/1117263/instruments-leaks-and-nsdateformatter">stackoverflow.com</a>, where it appears that 1 MB is lost after invoking NSFormatter -dateFromString.</p>
<p>I ran into the same situation, with very similar leak sizes to the ones reported on that article. Instruments shows that 868 Kb (889520 bytes to be exact) are allocated by Cocoa on iPhone OS (verified from 2.x to 3.x up to 3.1.3).<br />
<span id="more-231"></span></p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>1. Z Offending Code<br />
</h2>
<p>The objective of this method is to convert  a string into a date.<br />
Note that I do invoke -release on the NSDateFormatter object, and that the newly created NSDate is destroyed when the NSAutoreleasePool is drained. This is not where the problem lies.</p>
<p><span style="color: #808080;"><code>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];<br />
[dateFormatter setTimeZone:[ NSTimeZone timeZoneWithName:@"GMT"]];<br />
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss <span style="color: #ff0000;">z</span>"];<br />
[dateFormatter setLenient:YES];<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSDate *testDate = [dateFormatter dateFromString:string];<span style="color: #339966;"> // &lt;- Allocates 868 Kb</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;// Do something with testDate<br />
&nbsp;&nbsp;&nbsp;&nbsp;[pool drain];<br />
}<br />
[dateFormatter release];<br />
</code></span></p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>2. Discussion<br />
</h2>
<p>It appears that this is caused by the <span style="color: #ff0000;">z</span> option in setDateFormat.<br />
My guess is that Cocoa allocates exactly 8618 time-zone related objects that live in memory for the duration of the application.<br />
I do not know of any workaround, other than not using the &#8220;z&#8221; option.<br />
<code>[ dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss"];</code></p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>3. Instruments log<br />
</h2>
<p>Here is the allocation log for the first 3 of the 8618 objects, allocated all at once by a single invocation to dateFromString. Without going into too much details, suffice it to say that I used &#8220;Created &#038; Still Living&#8221; option in &#8220;Allocation Lifespan&#8221;, and that the &#8220;Extended Detail&#8221; view in Instruments shows that all those objects are created back to back as the result of the CoreFoundation CFDateFormatterGetAbsoluteTimeFromString method.</p>
<pre class="wp-code-highlight prettyprint">&lt;span style=&quot;color: #888888;&quot;&gt;

&lt;table border=&quot;0&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;#&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Address&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Category&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Time&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Size&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Library&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Caller&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2280&lt;/td&gt;
&lt;td&gt;0xa20be00&lt;/td&gt;
&lt;td&gt;GeneralBlock-32&lt;/td&gt;
&lt;td&gt;01:07.742&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;Foundation&lt;/td&gt;
&lt;td&gt;-[NSDateFormatter getObjectValue:forString:range:error:]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2281&lt;/td&gt;
&lt;td&gt;0xa219670&lt;/td&gt;
&lt;td&gt;GeneralBlock-176&lt;/td&gt;
&lt;td&gt;01:07.833&lt;/td&gt;
&lt;td&gt;176&lt;/td&gt;
&lt;td&gt;Foundation&lt;/td&gt;
&lt;td&gt;-[NSDateFormatter getObjectValue:forString:range:error:]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2282&lt;/td&gt;
&lt;td&gt;0xa298db0&lt;/td&gt;
&lt;td&gt;GeneralBlock-48&lt;/td&gt;
&lt;td&gt;01:07.833&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;Foundation&lt;/td&gt;
&lt;td&gt;-[NSDateFormatter getObjectValue:forString:range:error:]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/span&gt;</pre>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>4. All 8618 Objects<br />
</h2>
<h3>The <a title="Instruments Log" href="http://thegothicparty.com/dev/wp-content/extras/instruments-log.html" target="_blank">entire log</a> (8618 entries) can be viewed <a title="Instruments Log" href="http://thegothicparty.com/dev/wp-content/extras/instruments-log.html" target="_blank">here</a>.</h3>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>5. Conclusion</h2>
<p>The StackOverflow.com positively isolates the problem and the workaround. Do not use setDateFormat:@&#8221;EEE, d MMM yyyy HH:mm:ss <span style="color: #ff0000;">z</span>&#8220;, unless you are willing to loose 868 Kb for the rest of the lifetime of the application. The alternative is to not use the time zone which is, I agree, a pain: setDateFormat:@&#8221;EEE, d MMM yyyy HH:mm:ss&#8221;.</p>
<p>A possible approach is to test the string against &#8221; GMT&#8221;, and to only invoke &#8220;z&#8221; when not in GMT:<br />
<span style="color: #808080;"><code><br />
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];<br />
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];<br />
if( ![string hasSuffix:@" GMT"]) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;// If the string is not GMT, we must use time zone <img src='http://thegothicparty.com/dev/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /><br />
&nbsp;&nbsp;&nbsp;&nbsp;[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];<br />
} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss"];<br />
}<br />
</code></span></p>
<p>As of today&#8217;s date iPhone GB cost, your user either $0.005 or $0.01, depending on which device she or he bought.</p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<h2>6. External Links</h2>
<ul>
<li>stackoverflow.com<br />
<a href="http://stackoverflow.com/questions/1117263/instruments-leaks-and-nsdateformatter">http://stackoverflow.com/questions/1117263/instruments-leaks-and-nsdateformatter</a>
</li>
<li>
Developer.apple.com<br />
Your tracking number for this issue is Bug ID# <a href="https://bugreport.apple.com/cgi-bin/WebObjects/RadarWeb.woa/92/wo/3EQvovmScvH9F84H22jHSw/11.56">7745113</a></li>
</ul>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="3" />
<div style="clear: both;"><!-- reset --></div>
<p></em></p>
<div style="clear: both;"><!-- reset --></div>
<p><em> </em></p>
<p><script src="http://widgets.digg.com/buttons.js" type="text/javascript"></script></p>
<p><a onclick="window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=thegothicparty.com%2Fdev%2Fiphone%2Fnsdateformatter-memory-leak&amp;title=NSDateFormatter+memory+leak', 'delicious', 'toolbar=no,width=550,height=550'); return false;" href="http://delicious.com/save"><img style="border: 0pt none;" src="http://www.delicious.com/static/img/delicious.small.gif" alt="Delicious" width="16" height="16" />Bookmark this on Delicious</a></p>
<p><a class="DiggThisButton DiggCompact" rev="news, programming" href="http://digg.com/submit?url=http%3A%2F%2Fthegothicparty.com%2Fdev%2Fiphone%2Fnsdateformatter-memory-leak&amp;title=NSDateFormatter+memory+leak"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Drowning in NSAutoreleasePool</title>
		<link>http://thegothicparty.com/dev/article/nsautoreleasepool/</link>
		<comments>http://thegothicparty.com/dev/article/nsautoreleasepool/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 19:37:14 +0000</pubDate>
		<dc:creator>dev</dc:creator>
				<category><![CDATA[OS Development]]></category>
		<category><![CDATA[Tech Articles]]></category>

		<guid isPermaLink="false">http://thegothicparty.com/dev/?p=189</guid>
		<description><![CDATA[When developing in Cocoa for Mac OS X or for iPhone, it is easy to overlook objects that are dropped in the current NSAutoreleasePool, ultimately leading to a crash. The following article applies to the use of NSAutoreleasePool when garbage collection is not enabled. Summary Preventing over-release [ -retain &#38; -release] vs. [ -autorelease] Circumventing [...]]]></description>
			<content:encoded><![CDATA[<p>When developing in Cocoa for Mac OS X or for iPhone, it is easy to overlook objects that are dropped in the current NSAutoreleasePool, ultimately leading to a crash. The following article applies to the use of NSAutoreleasePool when garbage collection is not enabled.</p>
<p><span id="more-189"></span></p>
<p>Summary</p>
<ol>
<li>Preventing over-release</li>
<li>[ -retain &amp; -release] vs. [ -autorelease]</li>
<li>Circumventing -autorelease</li>
<li>NSAutoreleasePool is a stack object</li>
<li>NSAutoreleasePool pseudo-code</li>
<li>[pool release] vs. [pool drain]</li>
<li>External links</li>
</ol>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>1. Preventing over-release</h2>
<p>There seem to be some unpredictable stability behavior with objects that are released more than once (objects for which the -retainCount drops below 1). At times, some objects sent too many [ release ] will fail silently, whereas occasionally the host application will crash.</p>
<p>An application must balance the number of -retain and -release sent to a NSObject.</p>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;SomeObject * myObj = [[SomeObject &lt;span style=&quot;color: #333399;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;];
...
[myObj &lt;span style=&quot;color: #333399;&quot;&gt;release&lt;/span&gt;];&lt;/code&gt;</pre>
<p>To a certain extent, it would be better if over-release would never crash (akin to [ nil release ]) or always crash, thus not hiding bugs during the development stage. The combination of NSZombie and malloc_history is a wonderful tool to debug over-releasing. Read &#8220;Debugging Autorelease&#8221; on CocoaDev:<br />
<a title="DebuggingAutorelease" href="http://www.cocoadev.com/index.pl?DebuggingAutorelease">http://www.cocoadev.com/index.pl?DebuggingAutorelease</a><br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>2. [ -retain &amp; -release] vs. [ -autorelease]</h2>
<p>Sending -autorelease to an object allows to literally forget about it. [ someObject -autorelease] ensures that someObject with a -retainCount of 1 will remain retained for the life of the NSAutoreleasePool it belongs to, and will receive a -release message when such pool receives -drain or -release. Generally, the question is &#8220;Which NSAutoreleasePool ?&#8221;. While the answer is &#8220;The most nested one, Sir.&#8221;, not knowing may lead to surprising behaviors. A defensive approach may be to only create only one NSAutoreleasePool for the entire duration of the application, but this is certainly a costly and rarely practical answer.</p>
<p>-autorelease, contrarily to -release, can be invoked more than once without ill effects.</p>
<p>For some reason, once an object received the -autorelease message, the operation cannot be undone. There is no -unautorelease message. Nor is there a way to tell the NSAutoreleasePool to just forget about that object.</p>
<p>This can lead to the following situations:</p>
<ol>
<li>Preventing to save/release memory occupied by a given object</li>
<li>Preventing to delete that object at all before deleting the NSAutoreleasePool (sending one-too-many -release will cause the NSAutoreleasePool to send a -release to an already deallocated object, and crash)</li>
<li>Preventing the deletion of a given NSAutoreleasePool, since one cannot know what&#8217;s in it.</li>
</ol>
<p>Following design patterns while being aware of these limitations is generally not a problem. A suggested approach is to use small scopes with local NSAutoreleasePool any time granularity can be achieved. In Cocoa, Threads do just that.</p>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;{
&lt;span style=&quot;color: #800080;&quot;&gt;    NSAutoreleasePool&lt;/span&gt; * pool = [[&lt;span style=&quot;color: #800080;&quot;&gt;NSAutoreleasePool&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;];
    SomeObject * myObj = [[SomeObject &lt;span style=&quot;color: #333399;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;];
    [myObj &lt;span style=&quot;color: #333399;&quot;&gt;autorelease&lt;/span&gt;]; &lt;span style=&quot;color: #008000;&quot;&gt;// No need to remember about that object&lt;/span&gt;
    ...
    [pool &lt;span style=&quot;color: #333399;&quot;&gt;drain&lt;/span&gt;];
}&lt;/code&gt;</pre>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>3. Circumventing -autorelease</h2>
<p>Unfortunately, some Cocoa routines will, without a choice, drop objects it creates in the current NSAutoreleasePool.<br />
Example:</p>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;&lt;span style=&quot;color: #800080;&quot;&gt;UIImage&lt;/span&gt; * newImage = [&lt;span style=&quot;color: #800080;&quot;&gt;UIImage&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;imageWithCGImage&lt;/span&gt;:some_CGImageRef];&lt;/code&gt;</pre>
<p>As a result, the caller has little or no control of when the new object will be destroyed. It will likely be deallocated when the NSAutoreleasePool to which it belongs is deallocated, which is generally not predictable.</p>
<p>The NSAutoreleasePool seems to be nothing more than a linked list which sends a -release message to all objects it contains (regardless of their state) upon drain. In a managed memory environment, the pseudo-code of NSAutoreleasePool -drain could be simplified like this:</p>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) drain {
    [autoreleasePool &lt;span style=&quot;color: #333399;&quot;&gt;release&lt;/span&gt;];
}
&lt;/code&gt;</pre>
<p>Objects that for which the -retainCount is greater than 1 after the -drain will thus not be released. One can take an object out of a pool by retaining it, then releasing the pool entirely. Such object will be effectively removed from the NSAutoreleasePool.<br />
Example:</p>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;&lt;span style=&quot;color: #800080;&quot;&gt;UIImage&lt;/span&gt; * newImage = &lt;span style=&quot;color: #800080;&quot;&gt;nil&lt;/span&gt;;
{
&lt;span style=&quot;color: #800080;&quot;&gt;    NSAutoreleasePool&lt;/span&gt; * pool = [[&lt;span style=&quot;color: #800080;&quot;&gt;NSAutoreleasePool&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;];
    newImage = [[&lt;span style=&quot;color: #800080;&quot;&gt;UIImage&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;imageWithCGImage&lt;/span&gt;:some_CGImageRef] &lt;span style=&quot;color: #333399;&quot;&gt;retain&lt;/span&gt;];
    [pool &lt;span style=&quot;color: #333399;&quot;&gt;drain&lt;/span&gt;];
}
&lt;span style=&quot;color: #008000;&quot;&gt;// The  newImage* is now outside of the pool, and retained normally&lt;/span&gt;&lt;/code&gt;</pre>
<p>An example of this technique can be found on Björn Sållarp&#8217;s article &#8220;UIImage with round corners&#8221;:<br />
<a title="UIImage with round corners" href="http://blog.sallarp.com/iphone-uiimage-round-corners">http://blog.sallarp.com/iphone-uiimage-round-corners</a></p>
<p>Note that in the example above, a special scope has been declared around the NSAutoreleasePool creation and destruction. This is good practice since the pool is actually a stack object, and must be created and released in the same { }. Please read on.</p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>4. NSAutoreleasePool is a stack object</h2>
<p>By nature, an NSAutoreleasePool instance is stored on the stack (as opposed to the heap). As a direct consequence, the scope of these pools, and ultimately of the objects they reference, are tightly coupled with the methods that created them. Even though the pools (just like the invocation of methods) are nested, there is no virtual stack of NSAutorelease pools.</p>
<p>As the method that created a given pool goes out of scope, so does that very pool, and all the objects referenced by it will receive a single -release message. For an object to survive a pool drain, it must be explicitly retained. See paragraph 3, Circumventing -Autorelease.</p>
<p>Being a stack object also makes it <span style="text-decoration: underline;">illegal</span> to create an NSAutoreleasePool instance in a method, and to release it in another. This reason alone can serve as a good motivation for creating small, local and  nested NSAutoreleasePool rather than one gigantic one for the life of the Thread.</p>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>5. NSAutoreleasePool pseudo-code</h2>
<p>In a managed memory environment, the NSAutoreleasePool acts pretty much like a NSArray. The pool maintains strong references to its content. Objects added to the pool receive a -retain message.</p>
<h3>Possible pseudo-code for NSAutoreleasePool</h3>
<pre class="wp-code-highlight prettyprint">&lt;code&gt;
&lt;span style=&quot;color: #008000;&quot;&gt; // .h&lt;/span&gt;
&lt;span style=&quot;color: #800080;&quot;&gt;@interface&lt;/span&gt; NSAutoreleasePool2 : NSObject {
&lt;span style=&quot;color: #800080;&quot;&gt;@private&lt;/span&gt;
&lt;span style=&quot;color: #800080;&quot;&gt;    NSMutableArray&lt;/span&gt; * autoreleasePool;
}
-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) drain;
-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) addObject:(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;)anObject;
-(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;) autoreleaseObject:(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;)anObject;
&lt;span style=&quot;color: #800080;&quot;&gt;@end&lt;/span&gt;

&lt;span style=&quot;color: #008000;&quot;&gt;// .m&lt;/span&gt;
&lt;span style=&quot;color: #800080;&quot;&gt;@implementation&lt;/span&gt; NSAutoreleasePool2
-(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;)init {
&lt;span style=&quot;color: #800080;&quot;&gt;    if&lt;/span&gt; ((&lt;span style=&quot;color: #800080;&quot;&gt;self&lt;/span&gt; = [&lt;span style=&quot;color: #800080;&quot;&gt;super&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;] ) != &lt;span style=&quot;color: #800080;&quot;&gt;nil&lt;/span&gt; ) {
        autoreleasePool = [[&lt;span style=&quot;color: #800080;&quot;&gt;NSMutableArray&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #333399;&quot;&gt;init&lt;/span&gt;];
    }
&lt;span style=&quot;color: #800080;&quot;&gt;    return&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;self&lt;/span&gt;;
}
-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) dealloc {
    [&lt;span style=&quot;color: #800080;&quot;&gt;self&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;drain&lt;/span&gt;];
    [&lt;span style=&quot;color: #800080;&quot;&gt;super&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;dealloc&lt;/span&gt;];
}
-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) drain {
    [autoreleasePool &lt;span style=&quot;color: #333399;&quot;&gt;release&lt;/span&gt;];
}
-(&lt;span style=&quot;color: #800080;&quot;&gt;void&lt;/span&gt;) addObject:(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;)anObject {
    [autoreleasePool &lt;span style=&quot;color: #333399;&quot;&gt;addObjec&lt;/span&gt;t:anObject];
}
-(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;) autoreleaseObject:(&lt;span style=&quot;color: #800080;&quot;&gt;id&lt;/span&gt;)anObject {
&lt;span style=&quot;color: #800080;&quot;&gt;    if&lt;/span&gt;( ! [autoreleasePool &lt;span style=&quot;color: #333399;&quot;&gt;containsObject&lt;/span&gt;:anObject]) {
        [&lt;span style=&quot;color: #800080;&quot;&gt;self&lt;/span&gt; &lt;span style=&quot;color: #333399;&quot;&gt;addObject&lt;/span&gt;:anObject];
    }
&lt;span style=&quot;color: #800080;&quot;&gt;    return&lt;/span&gt; anObject;
}
&lt;span style=&quot;color: #800080;&quot;&gt;@end&lt;/span&gt;
&lt;/code&gt;</pre>
<h3>Discussion</h3>
<p>This is pseudo-code, not actual NSAutoreleasePool source code.<br />
Objects <span style="text-decoration: underline;">must</span> be added explicitly to this pool (using [pool <span style="color: #000000;">autoreleaseObject</span>:someObject ]; instead of [someObject autorelease];)<br />
-autoreleaseObject is lenient, and will not penalize objects added more than once.<br />
-addObject is strict ; objects added more than once will receive -release more than once.<br />
-drain and -release do not have the same behavior. This pseudo-code expects an invocation to [pool release ] rather than [pool drain ] to free the NSAutoReleasePool2.<br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>6. [pool release] vs. [pool drain]</h2>
<p>In a reference-counted environment sending -release or -drain to a NSAutoreleasePool has the same effect.</p>
<p>In a garbage-collection environment, without NSAutoreleasePool, -release has no effect while -drain triggers garbage collection. For this reason, the developer documentation explicitly recommends using  -drain, since it has a meaning in both management models, even on iPhone.</p>
<p>Read more about NSAutoreleasePool -drain on Apple&#8217;s developer site:<br />
<a title="NSAutoreleasePool Class Reference" href="http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html">http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html</a><br />
<em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="1" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<h2>7. External links</h2>
<ul>
<li><a title="DebuggingAutorelease" href="http://www.cocoadev.com/index.pl?DebuggingAutorelease">http://www.cocoadev.com/index.pl?DebuggingAutorelease</a></li>
<li><a title="UIImage with round corners" href="http://blog.sallarp.com/iphone-uiimage-round-corners/">http://blog.sallarp.com/iphone-uiimage-round-corners/</a></li>
<li><a title="NSAutoreleasePool Class Reference" href="http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html">http://developer.apple.com/&#8230;/NSAutoreleasePool_Class/&#8230;</a></li>
</ul>
<p><em><img class="size-full wp-image-335 alignleft" style="border: 0pt none;" title="orangewhite" src="http://thegothicparty.com/dev/wp-content/uploads/2010/03/orangewhite.png" alt="" width="500" height="3" /><br />
</em></p>
<p><em> </em></p>
<div style="clear: both;"><!-- reset --></div>
<p><script src="http://widgets.digg.com/buttons.js" type="text/javascript"></script></p>
<p><a onclick="window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=thegothicparty.com%2Fdev%2Fmacos%2Fnsautoreleasepool&amp;title=Drowning+in+NSAutoreleasePool', 'delicious', 'toolbar=no,width=550,height=550'); return false;" href="http://delicious.com/save"><img style="border: 0pt none;" src="http://www.delicious.com/static/img/delicious.small.gif" alt="Delicious" width="16" height="16" />Bookmark this on Delicious</a></p>
<p><a class="DiggThisButton DiggCompact" rev="news, programming" href="http://digg.com/submit?url=http%3A%2F%2Fthegothicparty.com%2Fdev%2Fmacos%2Fnsautoreleasepool&amp;title=Drowning+in+NSAutoreleasePool"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://thegothicparty.com/dev/article/nsautoreleasepool/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

