<?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: Memory Management and nil</title>
	<atom:link href="http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil</link>
	<description>Tips and Tricks for iPhone, iPod, iPad and iOS Developers</description>
	<lastBuildDate>Tue, 17 Apr 2012 16:54:31 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Nick</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-3638</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Sat, 03 Jul 2010 15:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-3638</guid>
		<description>@Abhinav: Setting a pointer to nil does not change the reference count and does nothing for memory management. The reason for setting pointers to nil after you have released the object the pointer points to is to avoid you trying to access that object via the pointer later in your code. If there are no other references to that object it will be released and the memory location for the object will be reclaimed. If you did not set your pointer to nil it will still point to the same memory location, which now may contain a very different object. This can lead to some confusing errors at runtime.

Of course you should not write code that tries to access objects that you have released. So in a perfect world, setting a pointer to nil after you release the object is not necessary. But being an imperfect programmer, I find it to be a useful failsafe mechanism and one that does not require a lot of effort.</description>
		<content:encoded><![CDATA[<p>@Abhinav: Setting a pointer to nil does not change the reference count and does nothing for memory management. The reason for setting pointers to nil after you have released the object the pointer points to is to avoid you trying to access that object via the pointer later in your code. If there are no other references to that object it will be released and the memory location for the object will be reclaimed. If you did not set your pointer to nil it will still point to the same memory location, which now may contain a very different object. This can lead to some confusing errors at runtime.</p>
<p>Of course you should not write code that tries to access objects that you have released. So in a perfect world, setting a pointer to nil after you release the object is not necessary. But being an imperfect programmer, I find it to be a useful failsafe mechanism and one that does not require a lot of effort.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhinav</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-3602</link>
		<dc:creator>Abhinav</dc:creator>
		<pubDate>Wed, 30 Jun 2010 20:56:52 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-3602</guid>
		<description>setting foostring to nil basically means setting the pointer as nil

We should not get confused that the object to which foostring pointed is still in memory.
we have just lost the reference to obj1 by setting as nil and since its refcount was 2 after obj2&#039;s retain, and refcount of foostring is 1 after calling the release and setting it as nil.
Although if the code is upto this only, i doubt that the object allocated actually still have a refcount 1. and we have set the obj1 to nil so we have actually lost its reference and cant further send release to reduce refcount to zero.

Isn&#039;t it a leek in that case.
However we are nt discussing leak here but i doubted it.
What are your comments to that.</description>
		<content:encoded><![CDATA[<p>setting foostring to nil basically means setting the pointer as nil</p>
<p>We should not get confused that the object to which foostring pointed is still in memory.<br />
we have just lost the reference to obj1 by setting as nil and since its refcount was 2 after obj2&#8217;s retain, and refcount of foostring is 1 after calling the release and setting it as nil.<br />
Although if the code is upto this only, i doubt that the object allocated actually still have a refcount 1. and we have set the obj1 to nil so we have actually lost its reference and cant further send release to reduce refcount to zero.</p>
<p>Isn&#8217;t it a leek in that case.<br />
However we are nt discussing leak here but i doubted it.<br />
What are your comments to that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sahrp mind</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-2322</link>
		<dc:creator>sahrp mind</dc:creator>
		<pubDate>Tue, 02 Mar 2010 17:18:58 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-2322</guid>
		<description>will these pointers be handled like c++</description>
		<content:encoded><![CDATA[<p>will these pointers be handled like c++</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Lorenz</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-2143</link>
		<dc:creator>Mark Lorenz</dc:creator>
		<pubDate>Tue, 16 Feb 2010 17:09:47 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-2143</guid>
		<description>@Geoffrey and @Trevor.  You are both correct.  Thanks for pointing that out.

I do still stand by my statement that it&#039;s generally preferable to crash rather than silently message nil.  Unless your program is setup in such a way that messages to nil are part of the program logic.</description>
		<content:encoded><![CDATA[<p>@Geoffrey and @Trevor.  You are both correct.  Thanks for pointing that out.</p>
<p>I do still stand by my statement that it&#8217;s generally preferable to crash rather than silently message nil.  Unless your program is setup in such a way that messages to nil are part of the program logic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trevor</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-2134</link>
		<dc:creator>Trevor</dc:creator>
		<pubDate>Tue, 16 Feb 2010 02:15:56 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-2134</guid>
		<description>@Mark: &quot;objectTwo tries to do an operation on fooString, but since objectOne set it to nil...&quot;

Wrong. You&#039;re confused about pointer copies. When objectOne passed fooString to objectTwo, it passed a *copy* of the fooString pointer. At this point objectOne has a copy of the fooString pointer, and objectTwo has its own distinct copy of the fooString pointer as well. Therefore, when objectOne sets its copy to nil, there is no effect on objectTwo&#039;s copy.</description>
		<content:encoded><![CDATA[<p>@Mark: &#8220;objectTwo tries to do an operation on fooString, but since objectOne set it to nil&#8230;&#8221;</p>
<p>Wrong. You&#8217;re confused about pointer copies. When objectOne passed fooString to objectTwo, it passed a *copy* of the fooString pointer. At this point objectOne has a copy of the fooString pointer, and objectTwo has its own distinct copy of the fooString pointer as well. Therefore, when objectOne sets its copy to nil, there is no effect on objectTwo&#8217;s copy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geoffrey Foster</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-2133</link>
		<dc:creator>Geoffrey Foster</dc:creator>
		<pubDate>Mon, 15 Feb 2010 22:59:18 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-2133</guid>
		<description>Mark, I respectfully disagree with you. You&#039;re actually wrong in the scenario that you described.

1) objectOne creates an NSString via [[NSString alloc] init]. This NSString object now has a retain count of 1
2) objectTwo is passed the NSString* and calls retain on it, the NSString object now has a retain count of 2
3) objectOne is done with the NSString  and calls release on it, the NSString object now has a retain count of 1. Setting the NSString* to nil is perfectly safe here
4) objectTwo tries to do an operation on the string, which still has a retain count of 1, thus this is perfectly okay.</description>
		<content:encoded><![CDATA[<p>Mark, I respectfully disagree with you. You&#8217;re actually wrong in the scenario that you described.</p>
<p>1) objectOne creates an NSString via [[NSString alloc] init]. This NSString object now has a retain count of 1<br />
2) objectTwo is passed the NSString* and calls retain on it, the NSString object now has a retain count of 2<br />
3) objectOne is done with the NSString  and calls release on it, the NSString object now has a retain count of 1. Setting the NSString* to nil is perfectly safe here<br />
4) objectTwo tries to do an operation on the string, which still has a retain count of 1, thus this is perfectly okay.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Lorenz</title>
		<link>http://iPhoneIncubator.com/blog/memory-management/memory-management-and-nil/comment-page-1#comment-2128</link>
		<dc:creator>Mark Lorenz</dc:creator>
		<pubDate>Mon, 15 Feb 2010 06:20:56 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=432#comment-2128</guid>
		<description>Respectfully, I have to disagree with you on setting object pointers to nil after calling release.

Since Objective-C uses a reference counting system to keep track of memory, it&#039;s very easy to see how the following scenario could play-out:
1) objectOne creates an NSString (i.e. NSString* fooString = [[NSString alloc] init];)

2) objectTwo is passed fooString, and calls [fooString retain]; because it&#039;s using the string, and needs it around.

3) objectOne is done with fooString, and so calls [fooString release];  Now, by your suggestion, it also does fooString = nil;

4) objectTwo tries to do an operation on fooString, but since objectOne set it to nil, the operation fails, and SILENTLY (because as you state, messages to nil are perfectly valid).  Now you have no idea why objectTwo isn&#039;t working correctly, and worse fooString may be been set to nil some time earlier, maybe even several run loops earlier, so it will be very difficult to figure out what&#039;s going wrong.  It can also be extremely difficult to figure out what object is setting fooString to nil in the first place.

In a reference counting memory management system, it&#039;s much smarter to let the run time do it&#039;s job, and keep track of those references.  If a fully released object is called you know immediately why the crash occurred, and then you just need to figure out where your reference counting went wrong.

One more thing, often times when a memory mis-management crash occurs the debugger will show objects you thought were one type as a different type entirely.  For example, something you though was a UIImage will show as an NSString in the debugger.  For this same reason it&#039;s also common to get &quot;Object Does Not Recognize Selector&quot; crashes, becuase your sending UIImage methods to an NSString (again, for example).

I hope you can see my point of view as a better choice than setting variables to nil.</description>
		<content:encoded><![CDATA[<p>Respectfully, I have to disagree with you on setting object pointers to nil after calling release.</p>
<p>Since Objective-C uses a reference counting system to keep track of memory, it&#8217;s very easy to see how the following scenario could play-out:<br />
1) objectOne creates an NSString (i.e. NSString* fooString = [[NSString alloc] init];)</p>
<p>2) objectTwo is passed fooString, and calls [fooString retain]; because it&#8217;s using the string, and needs it around.</p>
<p>3) objectOne is done with fooString, and so calls [fooString release];  Now, by your suggestion, it also does fooString = nil;</p>
<p>4) objectTwo tries to do an operation on fooString, but since objectOne set it to nil, the operation fails, and SILENTLY (because as you state, messages to nil are perfectly valid).  Now you have no idea why objectTwo isn&#8217;t working correctly, and worse fooString may be been set to nil some time earlier, maybe even several run loops earlier, so it will be very difficult to figure out what&#8217;s going wrong.  It can also be extremely difficult to figure out what object is setting fooString to nil in the first place.</p>
<p>In a reference counting memory management system, it&#8217;s much smarter to let the run time do it&#8217;s job, and keep track of those references.  If a fully released object is called you know immediately why the crash occurred, and then you just need to figure out where your reference counting went wrong.</p>
<p>One more thing, often times when a memory mis-management crash occurs the debugger will show objects you thought were one type as a different type entirely.  For example, something you though was a UIImage will show as an NSString in the debugger.  For this same reason it&#8217;s also common to get &#8220;Object Does Not Recognize Selector&#8221; crashes, becuase your sending UIImage methods to an NSString (again, for example).</p>
<p>I hope you can see my point of view as a better choice than setting variables to nil.</p>
]]></content:encoded>
	</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
Database Caching 3/11 queries in 0.013 seconds using disk: basic
Object Caching 295/297 objects using disk: basic

Served from: iphoneincubator.com @ 2012-05-17 04:32:13 -->
