<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2011-09-30-automatic-reference-counting.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Thu, 12 Nov 2026 23:39:19 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>CarmeloS - 2015-01-18 15:28:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;However, beware! ARC currently does not automatically copy a block literal that's converted to an id.
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;This seems to have been fixed by Apple by SDK 8.0, would you please verify this and note it on your article?
&lt;br /&gt;
&lt;br /&gt;Thanks.</description><guid isPermaLink="true">a089245fbfcb5e271fe3fa888351ea3e</guid><pubDate>Sun, 18 Jan 2015 15:28:13 GMT</pubDate></item><item><title>Tom Wagner - 2013-04-05 04:39:28</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I'm finding it hard to get a good answer about blocks and copy under ARC.  Apple says this in their "Transitioning to ARC Release Notes":
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;How do blocks work in ARC?
&lt;br /&gt;
&lt;br /&gt;Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use [^{} copy] when passing “down” the stack into arrayWithObjects: and other methods that do a retain.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;But you say that you don't need to copy unless it is being cast to id.  So was it that Apple didn't want to get into the real reason you need copy (because arrayWithObjects doesn't know it is a block) or is it true that you need to use [block copy] anytime you hold onto a block (vs when you return it up up the stack)?</description><guid isPermaLink="true">17f2c3741d22b45ef486fbf4bbe03b45</guid><pubDate>Fri, 05 Apr 2013 04:39:28 GMT</pubDate></item><item><title>Jay Bruins - 2012-09-13 02:30:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Looks like I equated "full-expression" to "scope" when I read that. Thanks for keeping me honest.
&lt;br /&gt;
&lt;br /&gt;Also, Apple's got your back:
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;For non-trivial cycles, however, you should use:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;MyViewController *myController = [[MyViewController alloc] init…];
&lt;br /&gt;// ...
&lt;br /&gt;MyViewController * __weak weakMyController = myController;
&lt;br /&gt;myController.completionHandler =  ^(NSInteger result) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MyViewController *strongMyController = weakMyController;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (strongMyController) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ...
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[strongMyController dismissViewControllerAnimated:YES completion:nil];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ...
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Probably nothing...
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;};&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;&lt;a href="http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW9"&gt;http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW9&lt;/a&gt;</description><guid isPermaLink="true">a75097472891387e7976d9826b5c31ff</guid><pubDate>Thu, 13 Sep 2012 02:30:21 GMT</pubDate></item><item><title>mikeash - 2012-09-13 02:10:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>"Full-expression" is not synonymous with a scope. It is roughly synonymous with "statement", expanded to also cover the expressions within control constructs like if statements, etc. See section 6.8 paragraph 4 of the C standard (or at least the one I have here) for the proper definition. The most relevant and clear part is probably:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;The end of a full expression is a sequence point.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;I'd never heard the term "full expression" in this context before, but we all know, or should know, what a sequence point is.
&lt;br /&gt;
&lt;br /&gt;Your quoted documentation says that, in the code we're discussing, the loaded value from _weakBar will be released as soon as the if statement decides which branch to take. That the compiler is more lenient is entirely chance.</description><guid isPermaLink="true">417617e7b18ba87d72a66d851ff7a7de</guid><pubDate>Thu, 13 Sep 2012 02:10:14 GMT</pubDate></item><item><title>Jay Bruins - 2012-09-12 22:15:23</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>That's a fair point, but that's assured by a different section of the same documentation.
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;Reading occurs when performing a lvalue-to-rvalue conversion on an object lvalue.
&lt;br /&gt;
&lt;br /&gt;For __weak objects, the current pointee is retained and &lt;b&gt;then released at the end of the current full-expression&lt;/b&gt;. This must execute atomically with respect to assignments and to the final release of the pointee.&lt;/div&gt;&lt;/div&gt; (emphasis mine)
&lt;br /&gt;
&lt;br /&gt;Thus, you're guaranteed to have a valid pointer for the rest of your scope. (You're correct that the autoreleased nature of objc_loadWeak is not necessary to achieve the above specification. A hidden strong variable could suffice.)
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#ownership.semantics"&gt;http://clang.llvm.org/docs/AutomaticReferenceCounting.html#ownership.semantics&lt;/a&gt;</description><guid isPermaLink="true">6f8e8c0ed57d7fbfe1d318e84fc235ac</guid><pubDate>Wed, 12 Sep 2012 22:15:23 GMT</pubDate></item><item><title>mikeash - 2012-09-12 14:15:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>That's how it's currently implemented, but unless the documentation guarantees that such an object's lifetime is extended to that of the current autorelease pool, you can't count on it. Without such a guarantee, nothing says that Clang couldn't decide to implement the if check using &lt;code&gt;objc_loadWeakRetained&lt;/code&gt; followed immediately by a release. If you're relying on Clang generating a call to &lt;code&gt;objc_loadWeak&lt;/code&gt; without any guarantee that that's what it must do, you're leaving your code open to breaking in future compiler releases.</description><guid isPermaLink="true">a7644e3c5fc8b2e386bfb825a9b4be33</guid><pubDate>Wed, 12 Sep 2012 14:15:11 GMT</pubDate></item><item><title>Jay Bruins - 2012-09-11 23:29:40</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I don't believe your analysis of this code is correct:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(_weakBar)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self mustNotBeNil: _weakBar];
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;From:
&lt;br /&gt;&lt;a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime.objc_loadWeak"&gt;http://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime.objc_loadWeak&lt;/a&gt;
&lt;br /&gt;(Accessed 9/11/2012)
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;8.8. id objc_loadWeak(id *object);
&lt;br /&gt;
&lt;br /&gt;Precondition: object is a valid pointer which either contains a null pointer or has been registered as a __weak object.
&lt;br /&gt;
&lt;br /&gt;If object is registered as a __weak object, and the last value stored into object has not yet been deallocated or begun deallocation, retains and autoreleases that value and returns it. Otherwise returns null. Equivalent to the following code:
&lt;br /&gt;
&lt;br /&gt;id objc_loadWeak(id *object) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;return objc_autorelease(objc_loadWeakRetained(object));
&lt;br /&gt;}
&lt;br /&gt;Must be atomic with respect to calls to objc_storeWeak on object.
&lt;br /&gt;
&lt;br /&gt;Rationale: loading weak references would be inherently prone to race conditions without the retain.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;This code:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bar *bar = _weakBar;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(bar)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self mustNotBeNil: bar];
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;however should result in a call to:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;8.9. id objc_loadWeakRetained(id *object);
&lt;br /&gt;
&lt;br /&gt;Precondition: object is a valid pointer which either contains a null pointer or has been registered as a __weak object.
&lt;br /&gt;
&lt;br /&gt;If object is registered as a __weak object, and the last value stored into object has not yet been deallocated or begun deallocation, retains that value and returns it. Otherwise returns null.
&lt;br /&gt;
&lt;br /&gt;Must be atomic with respect to calls to objc_storeWeak on object.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;Via disassembling a quick test app in lldb, I confirmed that these are the calls being generated in both cases.
&lt;br /&gt;
&lt;br /&gt;Thus, both versions of your code are safe, but for different reasons.</description><guid isPermaLink="true">9084bc48b401e60608748e19c3dd2538</guid><pubDate>Tue, 11 Sep 2012 23:29:40 GMT</pubDate></item><item><title>mikeash - 2012-09-11 21:19:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Your WeakValue class is not safe to use as a dictionary key in any case. The fact that it holds a weak reference that it uses to implement equality (and thus hashing) makes it mutable, and using mutable objects as dictionary keys is a serious problem.
&lt;br /&gt;
&lt;br /&gt;I think the only safe way to implement a weak dictionary key like this would be to use object identity rather than isEqual:, and have the value object cache its hash when it's created. You could then remove it by having a specialization that used __unsafe_unretained (completely safe as long as you don't message it).
&lt;br /&gt;
&lt;br /&gt;Another, perhaps better, way would be to write your own dictionary class (e.g. starting with MAMutableDictionary from &lt;a href="https://github.com/mikeash/MACollections"&gt;https://github.com/mikeash/MACollections&lt;/a&gt;) and having it use weak references directly, with the ability to locate and remove zeroed ones.</description><guid isPermaLink="true">a38abe2aa51c6fdefaff8dca24fe49bb</guid><pubDate>Tue, 11 Sep 2012 21:19:51 GMT</pubDate></item><item><title>Bill Cox - 2012-09-11 19:31:12</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Figured I'd let you in on something I'd discovered that I hadn't seen mentioned. It's related to this error:
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objc[2478]: cannot form weak reference to instance (0x10360f000) of class MyClass
&lt;br /&gt;
&lt;br /&gt;It seems like any time you try to form a weak reference to an object &lt;i&gt;after you reach the object's destructor&lt;/i&gt;, you can trigger this error. No override of retain or release needed, as your FAQ stated. 
&lt;br /&gt;
&lt;br /&gt;Explanation: 
&lt;br /&gt;
&lt;br /&gt;I have some code that stores weak references as the keys to a mutable dictionary (Think NSMapTable, but pre-iOS 6 so NSMapTable isn't available). When my objects are no longer needed in the dictionary, I want to remove the keys and their associated objects.
&lt;br /&gt;
&lt;br /&gt;I added some cleanup code to -dealloc and discovered something: My cleanup code tries to recreate the wrapped object to remove it from the dictionary, but it blows up with the same error above.
&lt;br /&gt;
&lt;br /&gt;I can fix it if I switch to __unsafe_unretained references, but unsafe pointers drive me crazy. I could also let the references go to zero and then clean them up later by looking for zeros, but this seemed less tidy. I've since solved it but figured somebody else might run into this problem.
&lt;br /&gt;
&lt;br /&gt;In case it's not clear, this is a simplified version of what I've got for my wrapper object:
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@interface WeakValue : NSObject 
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@property (weak, nonatomic) id nonretainedObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@end
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@implementation WeakValue
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@synthesize nonretainedObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;- (BOOL)isEqual:(WeakValue *)object { return [self.nonretainedObject isEqual:object.nonretainedObject; }
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@end
&lt;br /&gt;
&lt;br /&gt;And then a dealloc method that eventually calls a tiny block of code:
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WeakValue *key = [[WeakValue alloc] init];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;key.nonretainedObject = self; // It blows up at runtime here with "cannot form weak reference to instance" error
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[dictionaryWithWeakValueKeys removeObjectForKey:key];</description><guid isPermaLink="true">35457c1fa782876fe75c0a662ce1ed2e</guid><pubDate>Tue, 11 Sep 2012 19:31:12 GMT</pubDate></item><item><title>Peter N Lewis - 2012-07-30 09:41:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Great article as always Mike!
&lt;br /&gt;
&lt;br /&gt;One thing I note, which you haven't mentioned and I was astonished about is how amazingly well ARC works with C++ objects.  No, ARC wont manage your C++ objects, but you can use Objective C object pointers in your C++ objects (or structs, although that turns them into objects) and ARC will manage them so that they are released when the object is destructed.
&lt;br /&gt;
&lt;br /&gt;I know you're not font of C++, but this is actually quite amazingly well done, and certainly worth a note near where you talk about C structs.
&lt;br /&gt;
&lt;br /&gt;See &lt;a href="http://tinyurl.com/Objective-C-ARC"&gt;http://tinyurl.com/Objective-C-ARC&lt;/a&gt; for some details - I've verified it all works as expected.
&lt;br /&gt;
&lt;br /&gt;(The link is &lt;a href="http://www.informit.com/articles/article.aspx?p=1745876&amp;amp;amp"&gt;http://www.informit.com/articles/article.aspx?p=1745876&amp;amp;&lt;/a&gt;;seqNum=3 but I couldn't get the comment system to encode the link correctly after the ampersand)</description><guid isPermaLink="true">e286ef75484a73f8af2d730070d3e49e</guid><pubDate>Mon, 30 Jul 2012 09:41:13 GMT</pubDate></item><item><title>Max - 2012-03-07 18:18:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>At last, great explanations of ARC. Thanks! Much appreciated.</description><guid isPermaLink="true">a29b886a8298d630310cc3a4f0f82bec</guid><pubDate>Wed, 07 Mar 2012 18:18:16 GMT</pubDate></item><item><title>bruno - 2012-03-03 16:18:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>ok, why are you talking about autorelease pool?
&lt;br /&gt;have you idea when and where the weakMyObject became nil?
&lt;br /&gt;
&lt;br /&gt;Before a test, I've supposed that in line 8 weakMyObject must be nil!</description><guid isPermaLink="true">d6965c0d3b8823af2295d45afaf60dc5</guid><pubDate>Sat, 03 Mar 2012 16:18:11 GMT</pubDate></item><item><title>mikeash - 2012-03-02 16:35:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>There's no guarantee that the original dictionary in myObject becomes nil on line 6. It may have been autoreleased somewhere, for example, and will remain live until the autorelease pool is popped.
&lt;br /&gt;
&lt;br /&gt;The best way to see the generated code is to disassemble your binary. All of the inserted calls to objc_retain and friends will be there.</description><guid isPermaLink="true">b3af0f608e9d1966c61546d5fe1950e5</guid><pubDate>Fri, 02 Mar 2012 16:35:42 GMT</pubDate></item><item><title>Bruno - 2012-03-02 12:42:43</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Take a look in this code.
&lt;br /&gt;
&lt;br /&gt;1    __strong NSDictionary* myObject = [[NSDictionary alloc] initWithObjectsAndKeys:@"hello 1", @"message", nil];
&lt;br /&gt;2   __weak NSDictionary* weakMyObject = myObject;
&lt;br /&gt;3    
&lt;br /&gt;4    NSLog(@"Message before: %@", [weakMyObject objectForKey:@"message"]);
&lt;br /&gt;5    
&lt;br /&gt;6    myObject = [[NSDictionary alloc] initWithObjectsAndKeys:@"hello 2", @"message", nil];
&lt;br /&gt;7    
&lt;br /&gt;8    NSLog(@"Message after: %@", [weakMyObject objectForKey:@"message"]);
&lt;br /&gt;
&lt;br /&gt;In line 6 myObject points to a new NSDictionary, so I suppose that weakMyObject have to be nil in line 8 but it isn't. What is wrong in my head? When weakMyObject became null. 
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;another question: there is a way yo see the generated ARC's code?
&lt;br /&gt;</description><guid isPermaLink="true">cd0c054af7a7e2e93c5b9ab652c05316</guid><pubDate>Fri, 02 Mar 2012 12:42:43 GMT</pubDate></item><item><title>mikeash - 2012-02-13 22:21:01</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>ARC is definitely not able to do heap compaction. However, heap compaction is an implementation detail (albeit a really useful one) of GCs, not a directly outwardly visible feature.</description><guid isPermaLink="true">b820ed090ef1991e40d884051267a572</guid><pubDate>Mon, 13 Feb 2012 22:21:01 GMT</pubDate></item><item><title>Cody Rose - 2012-02-13 18:45:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Generational garbage collections also do heap compaction, which I can't see how ARC could do.</description><guid isPermaLink="true">03e2cd0542f819284093430de4f5ecfd</guid><pubDate>Mon, 13 Feb 2012 18:45:14 GMT</pubDate></item><item><title>mikeash - 2011-12-12 17:02:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>As far as I know, __weak variables are still immutable when captured. For your example, I'd suggest passing the operation as a parameter to the block so it doesn't have to rely on outside variables. Another option would be to capture a proxy object to the operation rather than capturing it directly.</description><guid isPermaLink="true">2afd4d0d673335861243b6ebd33b8899</guid><pubDate>Mon, 12 Dec 2011 17:02:51 GMT</pubDate></item><item><title>Stefan - 2011-12-11 22:03:04</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;Code that uses __block to avoid retain cycles won't work anymore. Instead, use __weak as described above.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;What if I need to avoid retain cycles &lt;i&gt;and&lt;/i&gt; allow the block to change a variable – with ARC?
&lt;br /&gt;
&lt;br /&gt;Is the __weak qualifier enough or is it necessary to use __block __weak?
&lt;br /&gt;
&lt;br /&gt;I am trying to update my cancellable NSBlockOperation which looked like this without ARC:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;__block NSBlockOperation *b = [NSBlockOperation blockOperationWithBlock:^{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (! [b isCancelled]) ...
&lt;br /&gt;}];&lt;/code&gt;</description><guid isPermaLink="true">adc3f3a2c916a90efc347772ea99b3e1</guid><pubDate>Sun, 11 Dec 2011 22:03:04 GMT</pubDate></item><item><title>Mazen - 2011-12-08 21:42:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Thank you for the great explanation!</description><guid isPermaLink="true">234874acc96742fb7df83bb24e3b12a0</guid><pubDate>Thu, 08 Dec 2011 21:42:08 GMT</pubDate></item><item><title>mikeash - 2011-10-28 14:47:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I think the main troubles will be cycles and CF bridging. I think you can track down cycles with Instruments.
&lt;br /&gt;
&lt;br /&gt;Replacing NSAllocateCollectable will work as long as you remember to implement both &lt;code&gt;dealloc&lt;/code&gt; and &lt;code&gt;finalize&lt;/code&gt; to free the stuff, in cases where it's tied to the lifetime of an object.
&lt;br /&gt;
&lt;br /&gt;I believe weak properties already worked under GC before ARC. The &lt;code&gt;__weak&lt;/code&gt; qualifier definitely works. I'm not sure if strong works, but if not, retain is equivalent.
&lt;br /&gt;
&lt;br /&gt;NSMakCollectable should work under ARC the same as it does under non-ARC non-GC, which is to say that it doesn't do anything. CFBridgingRelease is an inline function so you can see how it works. Under non-ARC it just does a standard CFMakeCollectable/autorelease combination.</description><guid isPermaLink="true">8fe45e0027c4c8c517af6711c0c4c256</guid><pubDate>Fri, 28 Oct 2011 14:47:39 GMT</pubDate></item><item><title>Sean McB - 2011-10-28 02:11:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Any thoughts on converting a GC app to ARC?
&lt;br /&gt;
&lt;br /&gt;(I'm also sad that Apple seems to be ditching GC.  Coding in GC one has less to worry about, though no doubt runtime performance will be somewhat better under ARC.)
&lt;br /&gt;
&lt;br /&gt;As changing the fundamental memory management model of a large codebase is not be be done lightly, I'm initially only interested in making ARC-friendly changes that are still valid in the GC case.
&lt;br /&gt;
&lt;br /&gt;Of the top of my head, my (rare) NSAllocateCollectable usage can change to malloc/free, and still work in both cases.
&lt;br /&gt;
&lt;br /&gt;Do the new strong/weak keywords work in GC?
&lt;br /&gt;
&lt;br /&gt;What does NSMakeCollectable do under ARC?  What does CFBridgingRelease do under GC?</description><guid isPermaLink="true">10f4c5c60bd3e7d2c8c412bb1c79eb10</guid><pubDate>Fri, 28 Oct 2011 02:11:20 GMT</pubDate></item><item><title>Scott - 2011-10-18 04:40:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Never mind -- I had set garbage collection to unsupported in projects, but not targets.</description><guid isPermaLink="true">07bf385d5279c9bc2b0b55e2ba708123</guid><pubDate>Tue, 18 Oct 2011 04:40:11 GMT</pubDate></item><item><title>Scott - 2011-10-18 04:10:41</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I've got a garbage-collected project that I'm converting to ARC.  Despite garbage collection being set to unsupported and ARC enabled, I'm getting analysis errors as if the project is still garbage collected.  Likewise, automatic conversion to ARC does not work because Xcode thinks that my project is still garbage collected.  Is this my fault or a bug in Xcode?</description><guid isPermaLink="true">9ab9c1cf039f07b66ae00d6e4ec84b46</guid><pubDate>Tue, 18 Oct 2011 04:10:41 GMT</pubDate></item><item><title>mikeash - 2011-10-16 04:00:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>My preferred route would be to use MAZeroingWeakRef wherever possible in that case.
&lt;br /&gt;
&lt;br /&gt;If you don't want the awful hackiness of that approach, you'll basically need to code like you would without ARC when it comes to weak references. Use __unsafe_unretained and be really paranoid about manually zeroing out weak references and not messaging stale pointers.</description><guid isPermaLink="true">da3a0285b8fb384ab4ff57730a3bc333</guid><pubDate>Sun, 16 Oct 2011 04:00:38 GMT</pubDate></item><item><title>ThomasW - 2011-10-15 15:55:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I've been wondering about iOS 4 compatibility. If you can't use weak zeroing references, what pattern should you follow if you want to use ARC on both iOS 4 and 5?</description><guid isPermaLink="true">d48829bf9826b47089e11a79dbee9ec0</guid><pubDate>Sat, 15 Oct 2011 15:55:35 GMT</pubDate></item><item><title>mikeash - 2011-10-10 18:23:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>A minor correction: ARC requires the "new runtime", which means Mac 64-bit or iOS (including simulator). Mac 32-bit is the only "old runtime" platform.</description><guid isPermaLink="true">b54652731381cad88de6637e22436b24</guid><pubDate>Mon, 10 Oct 2011 18:23:59 GMT</pubDate></item><item><title>mikeash - 2011-10-10 18:23:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;bill:&lt;/b&gt; You can target iOS 4 and Mac OS X 10.6 with ARC, but you can't use zeroing weak references there. Those require iOS5/10.7.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Ray:&lt;/b&gt; Yes, I believe ARC requires 64-bit, so you're probably out of luck. Note that the order of object destruction is something you should try to avoid relying upon anyway. Order is not guaranteed with ARC either, unless one of the objects has a strong reference to the other.</description><guid isPermaLink="true">c196c0ffc69366c46e04bf3204ad47e6</guid><pubDate>Mon, 10 Oct 2011 18:23:18 GMT</pubDate></item><item><title>Ray - 2011-10-10 17:28:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I was trying to change some code (a framework) from using garbage collection to arc as I had some bugs with garbage collection (I couldn't force a specific order that objects are destroyed) and thought the more deterministic nature of ARC would benefit more.... 
&lt;br /&gt;
&lt;br /&gt;However, I keep getting this error: "-fobjc-arc is not supported with fragile abi" on my project. What are the potential causes for this error? My framework uses Oracle's instant client c++ libraries so is objective-c++ as well as 32 bit only as the 64 bit instant client doesn't work on Lion yet. Does arc require 64 bit?</description><guid isPermaLink="true">38047a34171dde2b2f2c810fa5d7bee3</guid><pubDate>Mon, 10 Oct 2011 17:28:25 GMT</pubDate></item><item><title>Rob Rix - 2011-10-10 13:49:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Thanks for clarifying that, Mike. You’re right that I almost never see that done correctly.</description><guid isPermaLink="true">1b4988fb8e7e21461e6541cfd2abf629</guid><pubDate>Mon, 10 Oct 2011 13:49:18 GMT</pubDate></item><item><title>Roman Busyghin - 2011-10-10 07:50:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Sorry, Mike. I  have read last comments and got all the answers!</description><guid isPermaLink="true">44088e763d6f529eadae40e35ddae825</guid><pubDate>Mon, 10 Oct 2011 07:50:31 GMT</pubDate></item><item><title>Roman Busyghin - 2011-10-10 06:47:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>I have a question about zeroing weak references.
&lt;br /&gt;
&lt;br /&gt;As you noticed earlier zeroing weak references won't work under iOS 4 and I must use __unsafe_unretained when declaring delegates, right?
&lt;br /&gt;
&lt;br /&gt;Thus this will make working with delegates in before-ARC style? Just without automatic zeroing of weak reference?</description><guid isPermaLink="true">6713f8dd54ce015aa4ece2d6167fb2bb</guid><pubDate>Mon, 10 Oct 2011 06:47:14 GMT</pubDate></item><item><title>bill - 2011-10-10 02:01:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>This is iOS 5 only, right?</description><guid isPermaLink="true">0b321e70f806eb39eca600921d37f24a</guid><pubDate>Mon, 10 Oct 2011 02:01:09 GMT</pubDate></item><item><title>mikeash - 2011-10-10 01:24:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>You're correct that __unsafe_unretained is the same as a regular assignment done the old way, but most Cocoa programmers don't exercise the caution they should when using those. How much code have you seen that manually zeroes out delegate and data source pointers in a window controller's -dealloc. It's almost unheard of. And yet, it's absolutely required for correctness, because you can't know the order of object destruction, and the view might message its data source or delegate after the controller has been destroyed. In practice, this happens rarely, but it does happen, and is really annoying to track down.
&lt;br /&gt;
&lt;br /&gt;So yes, you should use the same amount of caution as dealing with both. However, that is most likely not the amount of caution that you're used to.</description><guid isPermaLink="true">4010c9e897b775d2d9a6f2eca3baa77e</guid><pubDate>Mon, 10 Oct 2011 01:24:15 GMT</pubDate></item><item><title>Rob Rix - 2011-10-10 00:28:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>It’s my understanding that __unsafe_unretained is effectively the same behaviour as “assign” for object pointers (e.g. delegates). If that is actually so, you still need to exercise caution, but it’s perhaps caution that you’re used to exercising for delegates or for breaking retain cycles.
&lt;br /&gt;
&lt;br /&gt;Mike, is that your understanding as well?</description><guid isPermaLink="true">770c95e4df3ae118f543a60962e24a74</guid><pubDate>Mon, 10 Oct 2011 00:28:20 GMT</pubDate></item><item><title>Daniel - 2011-10-08 10:50:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Sorry for the extreme brevity in my previous question:
&lt;br /&gt;I was posting from the phone — which actually ate my more verbose first draft :-(
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;@Mike&lt;/b&gt; When using performSelector* with a non-constant selector under ARC, you get a compiler warning that this may cause a leak — the exact warning Xcode gives you is:
&lt;br /&gt;&lt;code&gt;warning: performSelector may cause a leak because its selector is unknown [-Warc-performSelector-leaks,3]
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(void)[target performSelector:selector withObject:self];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;^
&lt;br /&gt;FILENAME:LINE:COLUMN: note: used here [3]
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(void)[target performSelector:selector withObject:self];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;^
&lt;br /&gt;1 warning generated.
&lt;br /&gt;&lt;/code&gt;.
&lt;br /&gt;(Which I now see how to get rid of, for the general case...)
&lt;br /&gt;
&lt;br /&gt;My particular case was a category on NSTimer — one that holds a weak reference to the actual timer-target through indirection.
&lt;br /&gt;So the #pragma solution &lt;b&gt;@David&lt;/b&gt; mentioned would definitely be an option — although much uglier than resorting to plain old  objc_msgSend().
&lt;br /&gt;
&lt;br /&gt;I hoped using one of the CLANG macros like NS_RETURNS_NOT_RETAINED might be an option, but they seem to only be available on method declaration and not inline, at the calling site.
&lt;br /&gt;Therefore, I tried defining and explicitly using the following protocol:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;@protocol WeakTimerTarget &amp;lt;NSObject&amp;gt;
&lt;br /&gt;- (id)performSelector:(SEL)selector withObject:(id)object NS_RETURNS_NOT_RETAINED;
&lt;br /&gt;@end
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;But that didn't do the trick, either.</description><guid isPermaLink="true">158d46a5ef6389d132120f071f51adff</guid><pubDate>Sat, 08 Oct 2011 10:50:32 GMT</pubDate></item><item><title>Jean-Denis - 2011-10-08 07:58:24</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Is there a way for debugging purposes to put a breakpoint where a giving weak reference is zeroed? Of should one rely on a watchpoint?
&lt;br /&gt;</description><guid isPermaLink="true">31d271dd9fb96446490183f41e942dd6</guid><pubDate>Sat, 08 Oct 2011 07:58:24 GMT</pubDate></item><item><title>David - 2011-10-08 00:22:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>@Daniel When LLVM issues a warning, it typically also states the command line switch you can use to disable the warning. You can then use a pragma to turn off that warning like so:
&lt;br /&gt;
&lt;br /&gt;#pragma clang diagnostic push
&lt;br /&gt;#pragma clang diagnostic ignored "&amp;lt;warning you want to disable&amp;gt;"
&lt;br /&gt;// Code that issues the warning
&lt;br /&gt;#pragma clang diagnostic pop
&lt;br /&gt;
&lt;br /&gt;Keep in mind that this is not something you should do to generally kill warnings. Warnings are absolutely there for a reason, and you should take great care before you disable one.
&lt;br /&gt;
&lt;br /&gt;In general, it is likely a good idea to move away from using -performSelector* calls to arbitrary selectors if possible – using blocks or trying to switch to a set of known selectors is generally preferable. Yes, this means that you may want to rethink your target-action pattern based code. At the same time, it is sometimes the correct pattern so don't be afraid to use it if you really think it is correct.</description><guid isPermaLink="true">ba6127c837d11205ab65fd9e055dad8d</guid><pubDate>Sat, 08 Oct 2011 00:22:08 GMT</pubDate></item><item><title>mikeash - 2011-10-07 20:45:23</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>What warnings are you referring to?</description><guid isPermaLink="true">36269020eb9f48807796746938766252</guid><pubDate>Fri, 07 Oct 2011 20:45:23 GMT</pubDate></item><item><title>Daniel - 2011-10-07 15:12:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Once again, a nice read.
&lt;br /&gt;
&lt;br /&gt;One question, though:
&lt;br /&gt;
&lt;br /&gt;How would one silence the warning on -[NSObject performSelector:withObject:] without resorting to objc_msgSend()?</description><guid isPermaLink="true">0aee54f98f42af4c184a74fdf8ae4f0f</guid><pubDate>Fri, 07 Oct 2011 15:12:39 GMT</pubDate></item><item><title>mikeash - 2011-10-03 23:30:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Note that the naming conventions are important, but only when mixing ARC and non-ARC code. If ARC-using code calls ARC-using code, it doesn't matter what your method is named, because both sides will follow the same convention, even if it's the "wrong" one. However, when ARC calls non-ARC, or the reverse, then you definitely need to get that right.</description><guid isPermaLink="true">21b3bb496d03cd65739e0a317acd9a46</guid><pubDate>Mon, 03 Oct 2011 23:30:26 GMT</pubDate></item><item><title>Preston - 2011-10-03 22:12:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Jason:
&lt;br /&gt;
&lt;br /&gt;According to the WWDC presentation, ARC is aware of camel case and will know that -newsFeedWithObject: is not a method returning ownership. I don't have iOS developer status and so can't test this in the current 4.2 beta, however.</description><guid isPermaLink="true">6d1f7092332b2f3551f21d0165f54263</guid><pubDate>Mon, 03 Oct 2011 22:12:42 GMT</pubDate></item><item><title>Jason - 2011-10-03 02:29:47</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>This article is great! One other item is to note how ARC is very particular about naming conventions, and that it will do the *wrong* thing if you break those conventions. For example: &lt;code&gt;[foo newsFeedWithObject:bar]&lt;/code&gt; will make ARC think you are returning a retained object since it has the 'new' prefix.</description><guid isPermaLink="true">329391ed214596fb03bef47ca9da3384</guid><pubDate>Mon, 03 Oct 2011 02:29:47 GMT</pubDate></item><item><title>Jonathan Sterling - 2011-10-02 22:25:04</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;mkeiser:&lt;/b&gt; Oh, I see your question. Sure, another part of your program that isn't using ARC could erroneously release that object. But that'd be really dumb, and would probably be picked up by the static analyzer anyway. But within the context of ARC, the object &lt;i&gt;will&lt;/i&gt; indeed stay alive.</description><guid isPermaLink="true">472579da1cf620cd8503f316439752b3</guid><pubDate>Sun, 02 Oct 2011 22:25:04 GMT</pubDate></item><item><title>Jonathan Sterling - 2011-10-02 22:23:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;mkseiser:&lt;/b&gt; It's because object pointers are implicitly &lt;code&gt;__strong&lt;/code&gt;. So, another way to write that would have been:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;__strong Bar *bar = _weakBar; 
&lt;br /&gt;if(bar) 
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self mustNotBeNil: bar]; 
&lt;br /&gt;&lt;/code&gt;</description><guid isPermaLink="true">f9e6fb370da56ccc009e4ac965e7ae97</guid><pubDate>Sun, 02 Oct 2011 22:23:00 GMT</pubDate></item><item><title>mkeiser - 2011-10-02 20:16:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description> The following statement confuses me a bit:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;&lt;code&gt;
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bar *bar = _weakBar;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(bar)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self mustNotBeNil: bar];
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;Because bar is a strong reference here, the object is guaranteed to stay alive (and the variable non-nil) throughout this code.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;What exactly do you mean with "the object is guaranteed to stay alive"? I understand that bar will never he zero'ed by ARC, but why can't the referenced object be released in another thread? It's a simple assignement operation, what exactly makes it a strong reference?</description><guid isPermaLink="true">d6d6f8fb81731c598c2041ecf8e76931</guid><pubDate>Sun, 02 Oct 2011 20:16:35 GMT</pubDate></item><item><title>mikeash - 2011-10-01 12:16:50</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;Scott Little:&lt;/b&gt; Understandable. However, consider: once a block has been copied to the heap, further copies do nothing but increment the reference count, so they are essentially free. ARC will automatically balance those not-copies with releases. So, while it sounds bad, there really is no harm in doing a &lt;code&gt;[block copy]&lt;/code&gt; where it's not necessary, when using ARC.</description><guid isPermaLink="true">49c152fe648264b0f4e633ba3ef57238</guid><pubDate>Sat, 01 Oct 2011 12:16:50 GMT</pubDate></item><item><title>Scott Little - 2011-10-01 09:53:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Nice article, very well explained.
&lt;br /&gt;
&lt;br /&gt;I think I have some unlearning to do though, since this sentence:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;There's no problem with throwing in an extra manual copy if you're unsure.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;made my spine crawl just a little bit :-p
&lt;br /&gt;
&lt;br /&gt;scott</description><guid isPermaLink="true">20e2fa0d4eaed0c5bfd26ca789029bd3</guid><pubDate>Sat, 01 Oct 2011 09:53:00 GMT</pubDate></item><item><title>mikeash - 2011-10-01 00:20:37</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;nevyn:&lt;/b&gt; I wonder how practical it would be for third parties to add a cycle detector on top of ARC to turn it into a real GC. Performance would probably not be the best, but it may not be completely ridiculous....
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Grzegorz Adam Hankiewicz:&lt;/b&gt; MAZeroingWeakRef can be used fine in ARC projects as long as those particular files are compiled as non-ARC. MAZWR is useful if you're using ARC but targeting 10.6/iOS4 where official ZWRs aren't available. If you're targeting 10.7/iOS5, then MAZWR doesn't offer any real advantage over the built-in stuff, and in fact it will use the built-in stuff when available.
&lt;br /&gt;
&lt;br /&gt;(The one potential advantage of MAZWR is that it can work with classes like NSWindow which don't work with the ARC version. Right now it doesn't, but in theory it could fall back to the dynamic subclass craziness for those classes. I'm not sure if this is worthwhile to build.)</description><guid isPermaLink="true">6bc7af38ab3f5147cfda78fef54562ca</guid><pubDate>Sat, 01 Oct 2011 00:20:37 GMT</pubDate></item><item><title>Grzegorz Adam Hankiewicz - 2011-09-30 22:33:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>How will ARC's weak references affect your MAZeroingWeakRef project? Will code using MAZeroingWeakRef convert to ARC without problems or should it be stripped in favor of the new __weak prefix?</description><guid isPermaLink="true">6a2064601657b5249bacd695cdb3d4a4</guid><pubDate>Fri, 30 Sep 2011 22:33:09 GMT</pubDate></item><item><title>Joshua Emmons - 2011-09-30 21:55:24</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Thanks so much for this explanation of toll-free bridging with ARC! I've been combing message boards, documentation, and headers for any sort of clue as to how this is supposed to work, and you elegant examples are the first to make sense to me.</description><guid isPermaLink="true">cdf7f51cd487118b88708e40344aa959</guid><pubDate>Fri, 30 Sep 2011 21:55:24 GMT</pubDate></item><item><title>nevyn - 2011-09-30 21:44:46</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Yay, I finally understand __bridge*! Thanks! Good block memory management summary too, I lazilly linked to it instead of writing about it myself from my blocks guide...
&lt;br /&gt;
&lt;br /&gt;I'm rather sad that the GC seems to be dying, I wonder if that means macruby's future suddenly turned dark :( Would so love to use that on iOS...</description><guid isPermaLink="true">43da41dc8f004030c7ef38114729a435</guid><pubDate>Fri, 30 Sep 2011 21:44:46 GMT</pubDate></item><item><title>mikeash - 2011-09-30 21:39:57</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>What I said can easily be verified by simply compiling some code and looking at the disassembly:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;-[TestClass foo]:
&lt;br /&gt;0000000100000dd0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pushq&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%rbp
&lt;br /&gt;0000000100000dd1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movq&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%rsp,%rbp
&lt;br /&gt;0000000100000dd4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movq&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0x0000040d(%rip),%rax
&lt;br /&gt;0000000100000ddb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movq&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(%rdi,%rax),%rdi
&lt;br /&gt;0000000100000ddf&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;popq&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%rbp
&lt;br /&gt;0000000100000de0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;jmp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0x100000e7a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;; symbol stub for: _objc_retainAutoreleaseReturnValue
&lt;br /&gt;0000000100000de5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nopl&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%cs:0x00000000(%rax,%rax)&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">4dbf80c1f25eead2700e58c7e8cc5d65</guid><pubDate>Fri, 30 Sep 2011 21:39:57 GMT</pubDate></item><item><title>mikeash - 2011-09-30 21:34:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>According to the Clang document I linked above, &lt;code&gt;objc_autoreleaseReturnValue&lt;/code&gt; just autoreleases the value passed to it, as you would expect. This is incorrect to do to an ivar returned from a basic accessor, and will lead to an over-release and likely subsequent crash.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;objc_retainAutoreleaseReturnValue&lt;/code&gt; does a retain and an autorelease, which is precisely what is needed here.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;objc_retainAutoreleasedReturnValue&lt;/code&gt; can be paired with either of the above. Which one of the above is needed depends entirely on the semantics of the called method. If the called method allocs (or retains or copies) something and then needs to return it to the caller, it will use &lt;code&gt;objc_autoreleaseReturnValue&lt;/code&gt;. If the called method is returning a value which it has &lt;i&gt;not&lt;/i&gt; alloced (or retained or copied), like an ivar, then it will use &lt;code&gt;objc_retainAutoreleaseReturnValue&lt;/code&gt; to do the retain/autorelease combination necessary for that case.</description><guid isPermaLink="true">62cac5e87acf8cb57b2ac1c6c5b522af</guid><pubDate>Fri, 30 Sep 2011 21:34:53 GMT</pubDate></item><item><title>ken - 2011-09-30 20:22:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>This one is correct:
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Foo *foo = objc_retainAutoreleasedReturnValue([self foo]);
&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;this one should be &lt;code&gt;objc_autoreleaseReturnValue&lt;/code&gt;.  
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;- (Foo *)foo
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return objc_retainAutoreleaseReturnValue(_foo);
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;They have to be paired.
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">2de6d95bdc68566c9533d3bd3284f3f7</guid><pubDate>Fri, 30 Sep 2011 20:22:33 GMT</pubDate></item><item><title>Mike Gerasimenko - 2011-09-30 19:47:54</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>If you would use the &lt;b&gt;"Leaks"&lt;/b&gt; tool with ARC it will show you the cycle references. 
&lt;br /&gt;
&lt;br /&gt;Also, in the nice graphical way:
&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/27714647/screen.png"&gt;http://dl.dropbox.com/u/27714647/screen.png&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;You can read about it (and other things about ARC) in the presentation here 
&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/27714647/ITJam2011%20ARC.key"&gt;http://dl.dropbox.com/u/27714647/ITJam2011%20ARC.key&lt;/a&gt;</description><guid isPermaLink="true">1f1b5a9882a7384526c367dc420ac6ef</guid><pubDate>Fri, 30 Sep 2011 19:47:54 GMT</pubDate></item><item><title>mikeash - 2011-09-30 19:30:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>My take on ARC has two facets.
&lt;br /&gt;
&lt;br /&gt;Taken entirely on its own, ARC is great. It gets rid of a ton of grunt work at a minimum of overhead and complexity. It's a perfect case where the compiler can step in and take a bunch of boring stuff away from the programmer.
&lt;br /&gt;
&lt;br /&gt;Taken in the larger context, where all of this work on ARC fairly heavily implies that Apple has given up on garbage collection, I'm sad. The ObjC GC has a lot of problems, but I was hopeful that it could be made better with time.</description><guid isPermaLink="true">678dc1a7b19a383232b7ec79d872491f</guid><pubDate>Fri, 30 Sep 2011 19:30:34 GMT</pubDate></item><item><title>charles - 2011-09-30 19:25:47</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Very nice write-up, thanks very much, Mike!
&lt;br /&gt;
&lt;br /&gt;So, what's your take on ARC: a good thing, a meh thing, 'good riddance GC', none of the above?... :-)</description><guid isPermaLink="true">470e6225ca8b0ea3dd63c690ee9502ec</guid><pubDate>Fri, 30 Sep 2011 19:25:47 GMT</pubDate></item><item><title>David - 2011-09-30 19:10:46</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>If you are exposing a Block property, you should always declare the property as copy, not strong. The current compiler has a bug where a Block property that is declared strong is retained instead of copied, which can lead to crashing issues.</description><guid isPermaLink="true">dc80b94897a95db72010dc734ea7bb7d</guid><pubDate>Fri, 30 Sep 2011 19:10:46 GMT</pubDate></item><item><title>mikeash - 2011-09-30 18:57:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;Dave:&lt;/b&gt; Alas, those are not publicly documented yet. I've actually found the casting modifiers growing on me while working on this stuff.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;ken:&lt;/b&gt; I'm pretty sure I want objc_retainAutoreleaseReturnValue, since an ivar accessor needs to do a retain/autorelease combo.
&lt;br /&gt;
&lt;br /&gt;Regarding the efficiency of the bridging functions, I thought they were just doing the same thing as the casts, so they should be the same, efficiency-wise.</description><guid isPermaLink="true">8e4b67e4816516383679d517040f6c9f</guid><pubDate>Fri, 30 Sep 2011 18:57:19 GMT</pubDate></item><item><title>ken - 2011-09-30 18:27:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Nice article! 
&lt;br /&gt;
&lt;br /&gt;In the places where you meant &lt;code&gt;objc_autoreleaseReturnValue&lt;/code&gt; you wrote &lt;code&gt; objc_retainAutoreleaseReturnValue&lt;/code&gt;.
&lt;br /&gt;
&lt;br /&gt;Also, Dave++.  If you use CFBridgingRetain and CFBridgingRelease, then the CF ref count looks balanced.  For example,
&lt;br /&gt;
&lt;br /&gt;NSString *str = CFBridgingRelease(CFStringCreate(...));
&lt;br /&gt;
&lt;br /&gt;I find the semantics much easier to keep track of, and it's efficient.  CFBridgingRelease is basically transfer one ref count from CF to objc, and the stack's strong, so in effect that is "don't generate any code, but make stuff look balanced".</description><guid isPermaLink="true">ad536d0fba93fb5b62b743a8a80180b6</guid><pubDate>Fri, 30 Sep 2011 18:27:58 GMT</pubDate></item><item><title>Brian - 2011-09-30 18:18:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Thanks Mike. This is much clearer than the Apple docs. The examples are very helpful.</description><guid isPermaLink="true">56f44ccdd0e3a5899a67c644f8fce146</guid><pubDate>Fri, 30 Sep 2011 18:18:25 GMT</pubDate></item><item><title>Dave - 2011-09-30 16:47:24</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>You should probably be using these functions instead of the casting modifiers:
&lt;br /&gt;
&lt;br /&gt;CFBridgingRetain()
&lt;br /&gt;CFBridgingRelease()</description><guid isPermaLink="true">7460b4d119b314541535f45cfa568e0b</guid><pubDate>Fri, 30 Sep 2011 16:47:24 GMT</pubDate></item><item><title>mikeash - 2011-09-30 16:36:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>&lt;b&gt;Preston:&lt;/b&gt; The only thing that separates ARC from being a true garbage collector is being able to handle retain cycles. Automatic refcounting plus a cycle collector is a pretty common way to do garbage collection, actually. IIRC both Python and PHP work this way, for example. However, I think that the benefit of automatic cycle management shouldn't be understated. Cycles can happen in places where you wouldn't expect, so there's definitely value in having them broken for you.
&lt;br /&gt;
&lt;br /&gt;However, the language around ARC makes it sound like Apple is planning to phase out GC in favor of ARC, so even though GC has advantages, these are probably outweighed by the fact that ARC code will have better longevity and portability.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Marc:&lt;/b&gt; Copy properties are fine, and are implicitly strong. Retain properties are also strong, but it's preferred that you explicitly use &lt;code&gt;strong&lt;/code&gt; instead. For overriding, all you need to do is make sure that the backing storage has the correct semantics. If you write your own accessors for a strong property, a normal ivar will do. For a weak property, a &lt;code&gt;__weak&lt;/code&gt; ivar will do it.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Iain:&lt;/b&gt; ARC only affects object pointers, so for primitives you can simply carry on as usual.</description><guid isPermaLink="true">af071ac5b3811d91794f571f2b926619</guid><pubDate>Fri, 30 Sep 2011 16:36:56 GMT</pubDate></item><item><title>Iain - 2011-09-30 16:30:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>What's the correct way to declare simple (number) properties with ARC?
&lt;br /&gt;I used to write @property(assign) BOOL flag; but now it sounds like assign should be avoided. But "unsafe-unretained' doesn't sound like it would be problematic with, say, an NSInteger ivar. Do I understand this correctly? </description><guid isPermaLink="true">b1258481e57afe11f277984922b183c8</guid><pubDate>Fri, 30 Sep 2011 16:30:11 GMT</pubDate></item><item><title>Marc - 2011-09-30 16:18:44</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>What about the previous retain/copy properties, how would one specify that you want a copy setter/getter synthesized with a strong property?
&lt;br /&gt;
&lt;br /&gt;Also, how would you override weak/strong synthesized properties with ARC?</description><guid isPermaLink="true">de981d8ce6cc1e60673a8a339b17709e</guid><pubDate>Fri, 30 Sep 2011 16:18:44 GMT</pubDate></item><item><title>Preston - 2011-09-30 15:57:48</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-09-30-automatic-reference-counting.html#comments</link><description>Other than the automatic breaking of retain cycles, do you believe that any practical reason exists to use garbage collection instead of ARC? Up until ARC's announcement, GC was still being evangelized by some Apple developers on the Cocoa mailing list and sites like StackOverflow, making ARC's announcement slightly surprising.</description><guid isPermaLink="true">e8c9bda16045548eff37420c6becd6f7</guid><pubDate>Fri, 30 Sep 2011 15:57:48 GMT</pubDate></item></channel></rss>
