<?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>Tewha &#187; cocoa</title>
	<atom:link href="http://tewha.net/tag/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://tewha.net</link>
	<description>Writings and links on iPhone and iPad programming</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:19:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Objective-C memory management</title>
		<link>http://tewha.net/2011/08/objective-c-memory-management/</link>
		<comments>http://tewha.net/2011/08/objective-c-memory-management/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 21:34:37 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[memory management]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1232</guid>
		<description><![CDATA[With Automatic Reference Counting (ARC) coming out soon, you could argue this post is coming almost too late. But there&#8217;s a lot of confusion over this, and I don&#8217;t think ARC will help much if you don&#8217;t understand the why &#8230; <a href="http://tewha.net/2011/08/objective-c-memory-management/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With Automatic Reference Counting (ARC) coming out soon, you could argue this post is coming almost too late. But there&#8217;s a lot of confusion over this, and I don&#8217;t think ARC will help much if you don&#8217;t understand the <em>why</em> of memory management.</p>

<p>After a couple years, I&#8217;ve come to adopt some very simple rules for memory management.</p>

<h2>Concepts</h2>

<p>The first step is to pick up a few concepts that will help you as you code.</p>

<ul>
<li><strong>Be lazy in memory management.</strong> Rely on the compiler to do everything it possibly can. The compiler can synthesize property setters that handle memory management according to the rules you set. Rely on this.</li>
<li><strong>Ownership.</strong> Think in terms of ownership, rather than reference counting. Thinking in terms of ownership makes problems and solutions obvious. Thinking in terms of ownership will help you keep in mind the <em>why</em> of what you&#8217;re doing, rather than the <em>what</em>. After you&#8217;ve got a good grasp of <em>why</em>, the <em>what</em> becomes obvious.</li>
<li><strong>Protect your objects.</strong> Own any object that you want to keep around as long as <code>self</code> is around.</li>
<li><strong>Avoid circular references.</strong> Never own an object that could own <code>self</code>. This means never owning <code>self</code>&#8216;s delegate or data source.</li>
</ul>

<h2>Practical guidelines</h2>

<p>But how does this translate to code? There&#8217;s a few specific and concrete things you can do to make this easier on yourself:</p>

<ul>
<li><strong>Use properties to centralize memory management rules.</strong> Use <code>@property</code> to establish the rules for memory management of an instance variable. (And <code>@synthesize</code> your properties to underscored instance variables to prevent naming conflicts.)</li>
<li><strong>Pick the right memory management rule.</strong> Use <code>retain</code> properties as your default. Data sources, delegates or other objects that might own the object you&#8217;re working in should be <code>assign</code> properties instead of <code>retain</code>.</li>
<li><strong>Rely on the centralized memory management rules.</strong> Use property set notation (<code>self.foo = nil;</code>) or the property setter (<code>[self setFoo: nil];</code>) everywhere you change the property&#8217;s value except in the property&#8217;s setter (if you need one) In this way, you rely on the <code>retain</code> or <code>assign</code> in your <code>@property</code> to specify the memory management. If you need to use <code>retain</code>, <code>release</code> or <code>autorelease</code> outside of a setter you&#8217;re doing too much. (And, also, you&#8217;ll have more work to do to be compatible with ARC.)</li>
<li><strong>Avoid being called once deallocated.</strong> In <code>dealloc</code>, <code>nil</code> any delegates you&#8217;ve set to <code>self</code>. (This eliminates the possibility that the object will survive longer than <code>self</code> and try to send messages to <code>self</code>.)</li>
<li><strong>Protect your private bits, too.</strong> Don&#8217;t use raw instance variables in objects requiring memory management. Use anonymous properties.</li>
<li><strong>Containers own their contents.</strong> You not only don&#8217;t need to but must not try to manage memory in a system container. An <code>NSArray</code> will send a <code>retain</code> to objects that are added to it, and a <code>release</code> to objects as they are removed. If you try to manually <code>retain</code> and <code>release</code> objects, you&#8217;ll miss a case.</li>
<li><strong>Verify.</strong> Analyze your code frequently. And profile your code early and often for leaks.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2011/08/objective-c-memory-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Annotating source for clang</title>
		<link>http://tewha.net/2011/02/annotating-source-for-clang/</link>
		<comments>http://tewha.net/2011/02/annotating-source-for-clang/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 19:58:29 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[better source code]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1155</guid>
		<description><![CDATA[You can annotate your sources for better clang results. Best use is probably to specify that certain parameters to your functions can&#8217;t be nil/null. (via Peter Hosey on Twitter.)]]></description>
			<content:encoded><![CDATA[<p>You can annotate your sources for <a href="http://clang-analyzer.llvm.org/annotations.html">better clang results</a>. Best use is probably to specify that certain parameters to your functions <a href="http://twitter.com/boredzo/status/36873262134726656">can&#8217;t be nil/null</a>. (via <a href="https:/twitter.com/boredzo">Peter Hosey</a> on Twitter.)</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2011/02/annotating-source-for-clang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C99 structure initialization</title>
		<link>http://tewha.net/2010/12/c99-structure-initialization/</link>
		<comments>http://tewha.net/2010/12/c99-structure-initialization/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 04:55:43 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C99]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1147</guid>
		<description><![CDATA[Dave Dribin (now of Apple): Fun with C99 Syntax. From May 2010, but I missed it then: C99 offers a better syntax than NSMakeRect.]]></description>
			<content:encoded><![CDATA[<p>Dave Dribin (now of Apple): <a href="http://www.dribin.org/dave/blog/archives/2010/05/15/c99_syntax/"> Fun with C99 Syntax</a>. From May 2010, but I missed it then: C99 offers a better syntax than NSMakeRect.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2010/12/c99-structure-initialization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C 2.2 Features</title>
		<link>http://tewha.net/2010/06/objective-c-2-2-features/</link>
		<comments>http://tewha.net/2010/06/objective-c-2-2-features/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 20:28:11 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[runtime]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1119</guid>
		<description><![CDATA[Martin Pilkington (Code Collector Pro) on Objective-C 2.2 features. He covers the code reduction features of the new runtime. These are coming to &#8220;modern&#8221; Apple runtimes, meaning 64-bit Mac and iPhone.]]></description>
			<content:encoded><![CDATA[<p>Martin Pilkington (Code Collector Pro) on <a href="http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/">Objective-C 2.2 features</a>. He covers the code reduction features of the new runtime. These are coming to &#8220;modern&#8221; Apple runtimes, meaning 64-bit Mac and iPhone.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2010/06/objective-c-2-2-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory and thread-safe custom property methods</title>
		<link>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/</link>
		<comments>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 19:28:50 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[threadsafe]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1079</guid>
		<description><![CDATA[Cocoa With Love (Matt Gallagher): Memory and thread-safe custom property methods]]></description>
			<content:encoded><![CDATA[<p>Cocoa With Love (Matt Gallagher): <a href="http://cocoawithlove.com/2009/10/memory-and-thread-safe-custom-property.html">Memory and thread-safe custom property methods</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safe, threaded design and inter-thread communication</title>
		<link>http://tewha.net/2009/08/safe-threaded-design-and-inter-thread-communication/</link>
		<comments>http://tewha.net/2009/08/safe-threaded-design-and-inter-thread-communication/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 15:42:49 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[notifications]]></category>
		<category><![CDATA[NSNotification]]></category>
		<category><![CDATA[NSNotificationCenter]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1032</guid>
		<description><![CDATA[Matt Gallagher (Cocoa with Love): Safe, threaded design and inter-thread communication]]></description>
			<content:encoded><![CDATA[<p>Matt Gallagher (Cocoa with Love): <a href="http://cocoawithlove.com/2009/08/safe-threaded-design-and-inter-thread.html">Safe, threaded design and inter-thread communication</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/08/safe-threaded-design-and-inter-thread-communication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Control and configuration of applications through Info.plist</title>
		<link>http://tewha.net/2009/08/control-and-configuration-of-applications-through-info-plist/</link>
		<comments>http://tewha.net/2009/08/control-and-configuration-of-applications-through-info-plist/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 19:11:16 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Info.post]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1019</guid>
		<description><![CDATA[Matt Gallagher (Cocoa with Love): Control and configuration of applications through Info.plist. I hadn&#8217;t heard of UIRequiresPersistentWiFi before, so this will be helpful to me.]]></description>
			<content:encoded><![CDATA[<p>Matt Gallagher (Cocoa with Love): <a href="http://cocoawithlove.com/2009/08/control-and-configuration-of.html">Control and configuration of applications through Info.plist</a>. I hadn&#8217;t heard of <code>UIRequiresPersistentWiFi</code> before, so this will be helpful to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/08/control-and-configuration-of-applications-through-info-plist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rules to avoid retain cycles</title>
		<link>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/</link>
		<comments>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 01:16:56 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[release retain]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=967</guid>
		<description><![CDATA[Matt Gallagher: Rules to avoid retain cycles]]></description>
			<content:encoded><![CDATA[<p>Matt Gallagher: <a href="http://cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html">Rules to avoid retain cycles</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging drawing performance</title>
		<link>http://tewha.net/2009/07/debugging-drawing-performance/</link>
		<comments>http://tewha.net/2009/07/debugging-drawing-performance/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:55:12 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Quartz]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=925</guid>
		<description><![CDATA[I knew about Quartz Debug.app , but hadn&#8217;t heard of NSShowAllDrawing. Sounds more practical.]]></description>
			<content:encoded><![CDATA[<p>I knew about Quartz Debug.app , but hadn&#8217;t heard of <a href="http://fruitstandsoftware.com/blog/2009/07/quick-and-easy-drawing-performance-debugging-with-nsshowalldrawing/">NSShowAllDrawing</a>. Sounds more practical.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/07/debugging-drawing-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSURL synchronous requests</title>
		<link>http://tewha.net/2009/07/nsurl-synchronous-requests/</link>
		<comments>http://tewha.net/2009/07/nsurl-synchronous-requests/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 18:06:15 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSURL]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=870</guid>
		<description><![CDATA[Can&#8217;t verify this yet, but something I want to investigate later: Chris Parker (who &#8220;works for a fruit company in Cupertino&#8221;) via Twitter: Hey iPhone devs: sending synchronous requests via NSURL on the main thread is a good way to &#8230; <a href="http://tewha.net/2009/07/nsurl-synchronous-requests/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Can&#8217;t verify this yet, but something I want to investigate later:</p>

<p><a href="http://twitter.com/ctp/status/2536073877">Chris Parker</a> (who &#8220;works for a fruit company in Cupertino&#8221;) via Twitter:</p>

<blockquote>Hey iPhone devs: sending synchronous requests via NSURL on the main thread is a good way to have SpringBoard kill you. Don&#8217;t do that.</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/07/nsurl-synchronous-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: tewha.net @ 2012-02-08 02:13:52 -->
