<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 11:28:27 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Sayeed Munawar Hussain - 2014-10-30 04:45:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Great post Mike. Thanks for creating Friday QA. Learned a lot here.  </description><guid isPermaLink="true">0476281b928bdd18e7b9cfc5e9e0e116</guid><pubDate>Thu, 30 Oct 2014 04:45:45 GMT</pubDate></item><item><title>bob - 2012-12-14 23:55:37</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>About the NSTimer and retain cycles, this only applies to timers with repeat:YES, right? Because if it does not repeat, it will invalidate itself after firing, at which point it will release its target, so it is not a permanent retain cycle.</description><guid isPermaLink="true">5d321400a8e52109619cf6ce4c185cdd</guid><pubDate>Fri, 14 Dec 2012 23:55:37 GMT</pubDate></item><item><title>Omar - 2012-11-02 19:34:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>I understand, thanks.</description><guid isPermaLink="true">98bc1f7c8ff331276a15cf9accdf1954</guid><pubDate>Fri, 02 Nov 2012 19:34:38 GMT</pubDate></item><item><title>mikeash - 2012-11-02 15:03:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>NSThread will retain the target for as long as the thread is alive. If you're terminating the thread before your object is supposed to be deallocated, you're fine. If you're telling the thread to exit in -dealloc, then you'll never get there.
&lt;br /&gt;
&lt;br /&gt;All Cocoa APIs need a periodic autorelease pool drain to clean up their stuff. If you're running your own long-lived thread, then you need an autorelease pool in your thread's top-level loop.</description><guid isPermaLink="true">b86aa73fec473481747699f4ba1cb284</guid><pubDate>Fri, 02 Nov 2012 15:03:32 GMT</pubDate></item><item><title>Omar - 2012-10-29 04:41:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>What about NSThread?
&lt;br /&gt;For example, if I have, in a viewDidLoad:
&lt;br /&gt;myThread = [NSThread detachNewThreadSelector: @selector(go) toTarge:self withObject:nil];
&lt;br /&gt;
&lt;br /&gt;And then I have
&lt;br /&gt;- (void) go
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;//do something
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;There is no cycle here, correct? When the go function ends, so does the Thread... or not?
&lt;br /&gt;
&lt;br /&gt;What if the go function were something like this:
&lt;br /&gt;
&lt;br /&gt;- (void) go
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;while (![NSThread isCancelled])
&lt;br /&gt;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//alloc something
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSData *d = [NSData dataWithContentsOfFile: @"somefilepath"];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[NSThread sleepForTimeInterval:1];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;I found out, in this case, that "d" is never deallocated, until the thread exits.
&lt;br /&gt;It seems that an @autoreleasepool{} is needed around the NSData allocation, for it to be properly released after each cycle. Is that correct?
&lt;br /&gt;
&lt;br /&gt;But still, if I have for example:
&lt;br /&gt;
&lt;br /&gt;- (IBAction) goBack
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;[myThread cancel];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;myThread = nil;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;[self.navigationController popViewControllerAnimated:YES];
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;This will still clear the thread, and the viewController itself, and all the NSData that were allocated, right?
&lt;br /&gt;
&lt;br /&gt;Anything else I should be aware when doing things like these?
&lt;br /&gt;
&lt;br /&gt;Thanks a lot for your very informative posts.
&lt;br /&gt;</description><guid isPermaLink="true">f270dcae9104d55fe0be487bf4f36845</guid><pubDate>Mon, 29 Oct 2012 04:41:14 GMT</pubDate></item><item><title>mikeash - 2010-05-29 23:04:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Using a setter to set to nil is just a convenient way to release the variable. For more information on this technique and the pros and cons of it, see this post:
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://www.mikeash.com/pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html"&gt;http://www.mikeash.com/pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html&lt;/a&gt;</description><guid isPermaLink="true">f29f8dfa825bbdf47d0c8ae643b38110</guid><pubDate>Sat, 29 May 2010 23:04:02 GMT</pubDate></item><item><title>Keith Selbee - 2010-05-29 10:46:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>In your first code example above, why are you setting _b's A ref to nil?  If this is the B dealloc there's no chance it's going to call A later, right?</description><guid isPermaLink="true">804036e370b0fc959c80804ec0fd6654</guid><pubDate>Sat, 29 May 2010 10:46:21 GMT</pubDate></item><item><title>mikeash - 2010-05-04 21:09:28</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>&lt;b&gt;jt:&lt;/b&gt; And thanks very much for saying so, always nice to see.</description><guid isPermaLink="true">8e9b3ee5afcd2b7d57eb824aa362c26d</guid><pubDate>Tue, 04 May 2010 21:09:28 GMT</pubDate></item><item><title>jt - 2010-05-01 20:35:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Mike: Just wanted to say how much I appreciate the Friday sessions.  Thanks.</description><guid isPermaLink="true">111649b2ceea52732bc969969f8e58cb</guid><pubDate>Sat, 01 May 2010 20:35:03 GMT</pubDate></item><item><title>James Walker - 2010-05-01 07:18:44</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Jeff, what's the advantage of using a notification, rather than having the target object have a nonretained pointer to the owner object, and sending it a message directly when the timer fires?</description><guid isPermaLink="true">970549b377654c826661947019c92bb0</guid><pubDate>Sat, 01 May 2010 07:18:44 GMT</pubDate></item><item><title>Anonymous - 2010-04-30 19:28:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Jeff, that's a really cool solution. Essentially you're adding an intermediary object (NSNotificationCenter) between the two objects involved, which breaks the retain-cycle by referring to your target via simple pointer assignment. Very clever!</description><guid isPermaLink="true">01bba5b2d877e7a902a93d3b85822629</guid><pubDate>Fri, 30 Apr 2010 19:28:26 GMT</pubDate></item><item><title>Jeff Johnson - 2010-04-30 17:30:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>I like to avoid timer retain cycles by creating a special timer target class that posts a notification when the timer fires. The owner of the timer observes the timer target notification. The timer retains the special target class rather than the owner of the timer, so there's no retain cycle.</description><guid isPermaLink="true">fa740961cfee5741f20a6dfd7db20d73</guid><pubDate>Fri, 30 Apr 2010 17:30:59 GMT</pubDate></item><item><title>Alex Blewitt - 2010-04-30 17:25:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Did you meain 'retaining' cycles for the subject of the post? There's  a missing 'in' somewhere, I think</description><guid isPermaLink="true">7a5390471c721ed90865e91662ce5253</guid><pubDate>Fri, 30 Apr 2010 17:25:02 GMT</pubDate></item><item><title>cygnl7 - 2010-04-30 16:54:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>Never mind.  I had the ownership backwards.</description><guid isPermaLink="true">0c32d2e9607bc2e9e479a7a6f22852b8</guid><pubDate>Fri, 30 Apr 2010 16:54:55 GMT</pubDate></item><item><title>cygnl7 - 2010-04-30 16:53:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html#comments</link><description>In the fixed NSTimer example, self is still the parameter to target.  Doesn't that still create a retain cycle?  Shouldn't the target be the new child Impl class?</description><guid isPermaLink="true">7485c4f9fb365656818cf95ce8bb26a9</guid><pubDate>Fri, 30 Apr 2010 16:53:26 GMT</pubDate></item></channel></rss>
