<?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</title>
	<atom:link href="http://iPhoneDeveloperTips.com/objective-c/memory-management.html/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Fri, 12 Mar 2010 20:00:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: John Muchow</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9997</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Wed, 24 Feb 2010 19:24:02 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9997</guid>
		<description>Hi Lukasz,

Like any object that you want to &quot;stick around&quot; you need to call retain to bump the reference count. The code bumps the reference count for the incoming object, releases the old object and then assigns the instance variable to the new object.

John</description>
		<content:encoded><![CDATA[<p>Hi Lukasz,</p>
<p>Like any object that you want to &#8220;stick around&#8221; you need to call retain to bump the reference count. The code bumps the reference count for the incoming object, releases the old object and then assigns the instance variable to the new object.</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lukasz Margielewski</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9985</link>
		<dc:creator>Lukasz Margielewski</dc:creator>
		<pubDate>Wed, 24 Feb 2010 16:36:53 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9985</guid>
		<description>First of all - this is a really great article.
However I do not understand one line in code:

-(void) setStr:(NSString *)input
{
  [input retain];  // Why ????
  [str release];
  str = input;
}

What is the purpose of: [input retain]; line for? And there is no release after all.
I thing I do not understand some basic thing here.

I would greately appreciate help with understanding it.</description>
		<content:encoded><![CDATA[<p>First of all &#8211; this is a really great article.<br />
However I do not understand one line in code:</p>
<p>-(void) setStr:(NSString *)input<br />
{<br />
  [input retain];  // Why ????<br />
  [str release];<br />
  str = input;<br />
}</p>
<p>What is the purpose of: [input retain]; line for? And there is no release after all.<br />
I thing I do not understand some basic thing here.</p>
<p>I would greately appreciate help with understanding it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9595</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Sun, 14 Feb 2010 15:30:44 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9595</guid>
		<description>Andrew,

The code example shown is how most all iPhone projects are setup inside the main method, where a pool is allocated and freed.

If you look at the documentation for &lt;strong&gt;NSAutoreleasePool&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;drain&lt;/em&gt;&lt;/strong&gt; states this:

&lt;em&gt;In a reference-counted environment, releases and pops the receiver; in a garbage-collected environment, triggers garbage collection if the memory allocated since the last collection is greater than the current threshold.

In a reference-counted environment, this method behaves the same as release.
&lt;/em&gt;

If you look at the documentation for release, it states this:

&lt;em&gt;&lt;strong&gt;Special Considerations&lt;/strong&gt;
You should typically use drain instead of release.
&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>Andrew,</p>
<p>The code example shown is how most all iPhone projects are setup inside the main method, where a pool is allocated and freed.</p>
<p>If you look at the documentation for <strong>NSAutoreleasePool</strong>, <strong><em>drain</em></strong> states this:</p>
<p><em>In a reference-counted environment, releases and pops the receiver; in a garbage-collected environment, triggers garbage collection if the memory allocated since the last collection is greater than the current threshold.</p>
<p>In a reference-counted environment, this method behaves the same as release.<br />
</em></p>
<p>If you look at the documentation for release, it states this:</p>
<p><em><strong>Special Considerations</strong><br />
You should typically use drain instead of release.<br />
</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9594</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Sun, 14 Feb 2010 15:17:59 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9594</guid>
		<description>Hi Andrew,

When an instance variable is set to nil using this approach [self setStr:nil],  the setter retains nil (effectively doing nothing), and as expected, releases the old value. Although somewhat confusing in syntax, this is a reasonable thing to do as the variable will now point to nil, not some random location in memory and the memory is freed.

If you just called str = nil, you would have a memory leak as the variable is being set to nil, however it is not being released.

Hope that helps.</description>
		<content:encoded><![CDATA[<p>Hi Andrew,</p>
<p>When an instance variable is set to nil using this approach [self setStr:nil],  the setter retains nil (effectively doing nothing), and as expected, releases the old value. Although somewhat confusing in syntax, this is a reasonable thing to do as the variable will now point to nil, not some random location in memory and the memory is freed.</p>
<p>If you just called str = nil, you would have a memory leak as the variable is being set to nil, however it is not being released.</p>
<p>Hope that helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Coad</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9576</link>
		<dc:creator>Andrew Coad</dc:creator>
		<pubDate>Sun, 14 Feb 2010 04:59:58 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9576</guid>
		<description>Actually, there is another problem with this code that i overlooked and that is the autorelease pool as follows:

int main(int argc, const char * argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      .
      .
      . 
  [pool drain];
  return 0;
}

Within the context of main() there are no objects that are being sent autorelease messages so creating a pool and destroying it is benign - it adds no value, it does no harm. For an introductory tutorial this could cause more confusion with newbies like myself - best to leave it out.

Also, use of:

[pool drain]

implies that this application exists within a garbage collected environment and hence all chatter about memory management is irrelevant. If the application is not running in a GC environment the the correct code is:

[pool release]</description>
		<content:encoded><![CDATA[<p>Actually, there is another problem with this code that i overlooked and that is the autorelease pool as follows:</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br />
      .<br />
      .<br />
      .<br />
  [pool drain];<br />
  return 0;<br />
}</p>
<p>Within the context of main() there are no objects that are being sent autorelease messages so creating a pool and destroying it is benign &#8211; it adds no value, it does no harm. For an introductory tutorial this could cause more confusion with newbies like myself &#8211; best to leave it out.</p>
<p>Also, use of:</p>
<p>[pool drain]</p>
<p>implies that this application exists within a garbage collected environment and hence all chatter about memory management is irrelevant. If the application is not running in a GC environment the the correct code is:</p>
<p>[pool release]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Coad</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9574</link>
		<dc:creator>Andrew Coad</dc:creator>
		<pubDate>Sun, 14 Feb 2010 04:30:58 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9574</guid>
		<description>As a newbie into this world, I can say that I do not understand the section in this thread that covers the release of &#039;str&#039; and the threat that &#039;str&#039; still points to a memory location. Maybe someone can point out where my misunderstanding is?

The sequence of events that is going on here is:

int main(int argc, const char * argv[])
{
            .
            .
            .
  TestClass *ptr = [[TestClass alloc] init];
            .
            .
            .

}

First event above is that an instance of TestClass is created; &#039;ptr&#039; now references that object.

int main(int argc, const char * argv[])
{
            .
            .
            .
  TestClass *ptr = [[TestClass alloc] init];
  [ptr setStr:@&quot;Fubar&quot;];
            .
            .
            .
}

Next, the instance variable of &#039;ptr&#039; (called &#039;str&#039; from above) is now set to reference an NSString object with the value &quot;Fubar&quot;.

int main(int argc, const char * argv[])
{
            .
            .
            .
  TestClass *ptr = [[TestClass alloc] init];
  [ptr setStr:@&quot;Fubar&quot;];
 
  [ptr release];
            .
            .
            .
}

Next, the destruction of the instance of TestClass as referenced by &#039;ptr&#039; begins. In the process of destroying &#039;ptr&#039;, the dealloc method of &#039;ptr&#039; is called which, from above is:

-(void) dealloc
{
  [str release];
  [super dealloc];
}
 
The dealloc method releases the instance variable &#039;str&#039; then calls [super dealloc] and the object no longer exists. At this point, it is not possible for any part of the class implementation (for this instance) to execute the code:

if (str)
  do something...
else
  do something else...

because the object no longer exists. In face the only point that I see there is a threat is is you inserted some code between:

[str release]

and

[super dealloc]

Yes, after [str release] is called but before [super dealloc] is called the condition where &#039;str&#039; points to a memory location which has already been deallocated is true. But since no one would do this the threat is zero. Am I not correct? What am I missing? Note that this is what the test code is doing: inspecting the value of &#039;str&#039; after deallocating the memory to which it refers but before [super dealloc] is called.

Looking at this a little deeper, if indeed the TestClass dealloc method was replaced with:

-(void) dealloc
{
  [self setStr:nil];
  [super dealloc];
}

You now have a memory leak, yes? The actual memory space allocated to hold the NSString &#039;str&#039; has not been released. All that has happened is that the reference to the NSString object has been wiped clean but NSString &#039;str&#039; still exists in all its glory.

One further question, within the context of main(), the &#039;str&#039; instance variable of TestClass has been set to &quot;Foobar&quot; as follows:

int main(int argc, const char * argv[])
{
            .
            .
            .
  TestClass *ptr = [[TestClass alloc] init];
  [ptr setStr:@&quot;Fubar&quot;];
            .
            .
            .
}

I assume, and please correct me if wrong, that this code snippet does NOT cause and instance of NSString with a reference count of 1 to be created within the context of main(), prior to the code inside setStr:(NSString *)s being executed. If this is not true then there is another memory leak since setStr:(NSString *)s retains the object which would bump its reference count to 2 and there is only one counteracting release in the lifecycle - the one in the dealloc method</description>
		<content:encoded><![CDATA[<p>As a newbie into this world, I can say that I do not understand the section in this thread that covers the release of &#8217;str&#8217; and the threat that &#8217;str&#8217; still points to a memory location. Maybe someone can point out where my misunderstanding is?</p>
<p>The sequence of events that is going on here is:</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
            .<br />
            .<br />
            .<br />
  TestClass *ptr = [[TestClass alloc] init];<br />
            .<br />
            .<br />
            .</p>
<p>}</p>
<p>First event above is that an instance of TestClass is created; &#8216;ptr&#8217; now references that object.</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
            .<br />
            .<br />
            .<br />
  TestClass *ptr = [[TestClass alloc] init];<br />
  [ptr setStr:@"Fubar"];<br />
            .<br />
            .<br />
            .<br />
}</p>
<p>Next, the instance variable of &#8216;ptr&#8217; (called &#8217;str&#8217; from above) is now set to reference an NSString object with the value &#8220;Fubar&#8221;.</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
            .<br />
            .<br />
            .<br />
  TestClass *ptr = [[TestClass alloc] init];<br />
  [ptr setStr:@"Fubar"];</p>
<p>  [ptr release];<br />
            .<br />
            .<br />
            .<br />
}</p>
<p>Next, the destruction of the instance of TestClass as referenced by &#8216;ptr&#8217; begins. In the process of destroying &#8216;ptr&#8217;, the dealloc method of &#8216;ptr&#8217; is called which, from above is:</p>
<p>-(void) dealloc<br />
{<br />
  [str release];<br />
  [super dealloc];<br />
}</p>
<p>The dealloc method releases the instance variable &#8217;str&#8217; then calls [super dealloc] and the object no longer exists. At this point, it is not possible for any part of the class implementation (for this instance) to execute the code:</p>
<p>if (str)<br />
  do something&#8230;<br />
else<br />
  do something else&#8230;</p>
<p>because the object no longer exists. In face the only point that I see there is a threat is is you inserted some code between:</p>
<p>[str release]</p>
<p>and</p>
<p>[super dealloc]</p>
<p>Yes, after [str release] is called but before [super dealloc] is called the condition where &#8217;str&#8217; points to a memory location which has already been deallocated is true. But since no one would do this the threat is zero. Am I not correct? What am I missing? Note that this is what the test code is doing: inspecting the value of &#8217;str&#8217; after deallocating the memory to which it refers but before [super dealloc] is called.</p>
<p>Looking at this a little deeper, if indeed the TestClass dealloc method was replaced with:</p>
<p>-(void) dealloc<br />
{<br />
  [self setStr:nil];<br />
  [super dealloc];<br />
}</p>
<p>You now have a memory leak, yes? The actual memory space allocated to hold the NSString &#8217;str&#8217; has not been released. All that has happened is that the reference to the NSString object has been wiped clean but NSString &#8217;str&#8217; still exists in all its glory.</p>
<p>One further question, within the context of main(), the &#8217;str&#8217; instance variable of TestClass has been set to &#8220;Foobar&#8221; as follows:</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
            .<br />
            .<br />
            .<br />
  TestClass *ptr = [[TestClass alloc] init];<br />
  [ptr setStr:@"Fubar"];<br />
            .<br />
            .<br />
            .<br />
}</p>
<p>I assume, and please correct me if wrong, that this code snippet does NOT cause and instance of NSString with a reference count of 1 to be created within the context of main(), prior to the code inside setStr:(NSString *)s being executed. If this is not true then there is another memory leak since setStr:(NSString *)s retains the object which would bump its reference count to 2 and there is only one counteracting release in the lifecycle &#8211; the one in the dealloc method</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9572</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Sun, 14 Feb 2010 04:17:03 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9572</guid>
		<description>Hi Waleska,

I was referring to a developer inadvertently referring to &#039;str&#039; after the variable has been released. If the variable is not set to nil, somewhere else in the code you could check the variable to see if it is nil as a means to know if it&#039;s been deallocated, which could result in an attempt to access a variable that has been released.</description>
		<content:encoded><![CDATA[<p>Hi Waleska,</p>
<p>I was referring to a developer inadvertently referring to &#8217;str&#8217; after the variable has been released. If the variable is not set to nil, somewhere else in the code you could check the variable to see if it is nil as a means to know if it&#8217;s been deallocated, which could result in an attempt to access a variable that has been released.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Waleska</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-9546</link>
		<dc:creator>Waleska</dc:creator>
		<pubDate>Sat, 13 Feb 2010 09:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-9546</guid>
		<description>Hi,
Congratulations for the article. It&#039;s very good! I didn&#039;t understand: &quot;The dealloc method in TestClass releases the memory for the ’str’ instance variable, and it seems all is well. However, if at some point, somewhere inside the class implementation an attempt was made to check if the ’str’ instance variable is nil (prior to doing some operation) for example:&quot; 

The dealloc method won&#039;t be execute only in the end? How at some point inside the class an attempt was made to check th &#039;str&#039; AFTER dealloc? Is it possible?? I&#039;m new in MAC programming..</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Congratulations for the article. It&#8217;s very good! I didn&#8217;t understand: &#8220;The dealloc method in TestClass releases the memory for the ’str’ instance variable, and it seems all is well. However, if at some point, somewhere inside the class implementation an attempt was made to check if the ’str’ instance variable is nil (prior to doing some operation) for example:&#8221; </p>
<p>The dealloc method won&#8217;t be execute only in the end? How at some point inside the class an attempt was made to check th &#8217;str&#8217; AFTER dealloc? Is it possible?? I&#8217;m new in MAC programming..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ankit</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-8591</link>
		<dc:creator>Ankit</dc:creator>
		<pubDate>Mon, 11 Jan 2010 06:19:10 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-8591</guid>
		<description>very well explained.....

superb.....

Keep it up!!!!</description>
		<content:encoded><![CDATA[<p>very well explained&#8230;..</p>
<p>superb&#8230;..</p>
<p>Keep it up!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kenneth</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/memory-management.html/comment-page-1#comment-5997</link>
		<dc:creator>Kenneth</dc:creator>
		<pubDate>Mon, 26 Oct 2009 12:41:19 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=46#comment-5997</guid>
		<description>Hi again John,

Sorry for the delayed response!

What I don&#039;t understand though is... isn&#039;t dealloc only run on an object when it is being removed from memory (ie, at the end of its life)? If the object in this example has itself been removed from memory, how would it be at all possible to access its str property?

Kenneth</description>
		<content:encoded><![CDATA[<p>Hi again John,</p>
<p>Sorry for the delayed response!</p>
<p>What I don&#8217;t understand though is&#8230; isn&#8217;t dealloc only run on an object when it is being removed from memory (ie, at the end of its life)? If the object in this example has itself been removed from memory, how would it be at all possible to access its str property?</p>
<p>Kenneth</p>
]]></content:encoded>
	</item>
</channel>
</rss>
