<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Cocoa memory management for smarties, Part 3: accessors</title>
	<atom:link href="http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/feed/" rel="self" type="application/rss+xml" />
	<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/</link>
	<description>Coding under the close supervision of cats</description>
	<lastBuildDate>Fri, 09 Jul 2010 18:22:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Jeff</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9256</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Wed, 08 Oct 2008 16:09:05 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9256</guid>
		<description>&lt;p&gt;Dave, it&#039;s true that both approaches would have the same result. The equality check is a minor optimization, because pointer comparison is faster than Objective-C messages. In most cases, though, it probably doesn&#039;t matter. If performance is an issue, there are various ways of optimizing the setter.&lt;/p&gt;
&lt;p&gt;Scott, it should be clear from my post that the accessors were never intended to be thread-safe.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Dave, it&#8217;s true that both approaches would have the same result. The equality check is a minor optimization, because pointer comparison is faster than Objective-C messages. In most cases, though, it probably doesn&#8217;t matter. If performance is an issue, there are various ways of optimizing the setter.</p>
<p>Scott, it should be clear from my post that the accessors were never intended to be thread-safe.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9237</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Mon, 06 Oct 2008 17:45:11 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9237</guid>
		<description>I have read at some point that in a multithreaded app it is possible that one thread can actually return a released object if it hits at the wrong time:
For example in your setter....

	if (_thing != aThing)
	{
		[_thing release];
                 // if your getter hit here on a different thread -- you have problems.
		_thing = [aThing retain];
	}


A safer approach is to release the old object AFTER the the pointer to the new object has been assigned.  use a local pointer to keep the reference of the ivar so it can be released afterwards

-(void) setThing:(id)aThing{
    if (_thing!= aThing){
        id oldThing = _thing;
        _thing = [aThing retain];
        [oldThing release];
    }
}</description>
		<content:encoded><![CDATA[<p>I have read at some point that in a multithreaded app it is possible that one thread can actually return a released object if it hits at the wrong time:<br />
For example in your setter&#8230;.</p>
<p>	if (_thing != aThing)<br />
	{<br />
		[_thing release];<br />
                 // if your getter hit here on a different thread &#8212; you have problems.<br />
		_thing = [aThing retain];<br />
	}</p>
<p>A safer approach is to release the old object AFTER the the pointer to the new object has been assigned.  use a local pointer to keep the reference of the ivar so it can be released afterwards</p>
<p>-(void) setThing:(id)aThing{<br />
    if (_thing!= aThing){<br />
        id oldThing = _thing;<br />
        _thing = [aThing retain];<br />
        [oldThing release];<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9233</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Mon, 06 Oct 2008 14:04:55 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9233</guid>
		<description>This code:
-(void) setThing:(id)aThing
{
	if (_thing != aThing)
	{
		[_thing release];
		_thing = [aThing retain];
	}
}

Can be easily rewritten as:
-(void) setThing:(id)aThing {
	[aThing retain];
	[_thing release];
	_thing = aThing;
}</description>
		<content:encoded><![CDATA[<p>This code:<br />
-(void) setThing:(id)aThing<br />
{<br />
	if (_thing != aThing)<br />
	{<br />
		[_thing release];<br />
		_thing = [aThing retain];<br />
	}<br />
}</p>
<p>Can be easily rewritten as:<br />
-(void) setThing:(id)aThing {<br />
	[aThing retain];<br />
	[_thing release];<br />
	_thing = aThing;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9221</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Mon, 06 Oct 2008 05:01:57 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9221</guid>
		<description>&lt;p&gt;Michal, thanks!&lt;/p&gt;
&lt;p&gt;Andrew, that&#039;s a good question. Suppose that on line 2 you have &lt;code&gt;[updater updateThings]&lt;/code&gt; instead of &lt;code&gt;[myThing setThing:nil]&lt;/code&gt;, where &lt;code&gt;updateThings&lt;/code&gt; contains the following code:&lt;/p&gt;
&lt;pre&gt;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
...
[[allThings objectAtIndex:i] setThing:nil];
...
[pool release];
&lt;/pre&gt;
&lt;p&gt;Since the setter&#039;s &lt;code&gt;[_thing autorelease]&lt;/code&gt; occurs within the context of this nested pool, &lt;code&gt;_thing&lt;/code&gt; could still be deallocated before line 3.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Michal, thanks!</p>
<p>Andrew, that&#8217;s a good question. Suppose that on line 2 you have <code>[updater updateThings]</code> instead of <code>[myThing setThing:nil]</code>, where <code>updateThings</code> contains the following code:</p>
<pre>
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
...
[[allThings objectAtIndex:i] setThing:nil];
...
[pool release];
</pre>
<p>Since the setter&#8217;s <code>[_thing autorelease]</code> occurs within the context of this nested pool, <code>_thing</code> could still be deallocated before line 3.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9213</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Sun, 05 Oct 2008 21:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9213</guid>
		<description>Admittedly I&#039;m still a bit of a Cocoa beginner, but why wouldn&#039;t changing [_thing release] to [_thing autorelease] in the setter suffice?</description>
		<content:encoded><![CDATA[<p>Admittedly I&#8217;m still a bit of a Cocoa beginner, but why wouldn&#8217;t changing [_thing release] to [_thing autorelease] in the setter suffice?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michal</title>
		<link>http://lapcatsoftware.com/blog/2008/10/05/cocoa-memory-management-for-smarties-part-3-accessors/comment-page-1/#comment-9207</link>
		<dc:creator>Michal</dc:creator>
		<pubDate>Sun, 05 Oct 2008 18:05:43 +0000</pubDate>
		<guid isPermaLink="false">http://lapcatsoftware.com/blog/?p=96#comment-9207</guid>
		<description>Never read such funny and still true intro to cocoa&#039;s memory management. And I&#039;ve read a bunch of them since I started with objective-c in middle 90s.</description>
		<content:encoded><![CDATA[<p>Never read such funny and still true intro to cocoa&#8217;s memory management. And I&#8217;ve read a bunch of them since I started with objective-c in middle 90s.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
