last day (15 days later) » 

4:49 PM
2
A: Retain Cycle Even when using Weak/Strong ARC Semantics

RobNo, you do not necessarily have a retain cycle (now known as a "strong reference cycle"). You have code that, if foo existed by the time strongSelf was defined, foo will be retained until the dispatched code finishes. The only potential retain cycle here is the delegate you passed to foo. If th...

 
Rob
The dealloc method can be removed in ARC regardless. If the delegate was weak, it's just not necessary. If the delegate was strong, you have a strong reference cycle (bad, bad, bad) and the dealloc would never get called, anyway (!).
 
Thanks for all the great input, I'm learning a ton here. So the delegate property of Foo is defined as (nonatomic, assign). This code base was migrated too ARC, I think that's why we have the older property attributes.
 
Rob
It means __unsafe_unretained, which means that it won't retain the object, but will leave a dangling pointer if the object is deallocated. That's why weak is so much better, that that pointer will be set to nil when the delegate is released, thereby eliminating whole categories of types of crashes. Replace that assign with weak.
 
@Rob by the way the reason I'm even chasing this is I'm relying on foo to be set to nil in dealloc to trigger Foo's dealloc which sets the delegate to nil. What I'm seeing is an async operation inside foo completes after the dealloc. Then inside the completion block I call on the delegate and of course get an access violation.
 
Rob
@Ternary No. When this object is released, it will automatically release its strong reference to foo anyway. Your dealloc was appropriate in pre-ARC code, but ARC eliminates the need for that dealloc method, and it's entirely redundant. Won't hurt, but should be removed in ARC to simplify your code. BTW, if that dispatch block is still running, the dispatch block, itself, will retain foo, so whether you nil it in dealloc or not, there is still one strong reference cycle (the dispatch block) that will keep it around.
If you want foo to be stopped and deallocated when this object is released, you have to implement your own cancellation logic within foo, which you can call from dealloc. A common pattern is to implement a cancel method which you'd call from dealloc. You'd then have runTests check frequently to see if foo was cancelled, and if so, terminate the tests immediately and return (thus ending the dispatch block and resolving that remaining strong reference).
 
4:49 PM
@Rob great explanation, I see the benefits of the weak now. Will test now and report back. Will this be easier with swift? :)
Changed my delegate to (atomic, weak) and when the async operation complete the delegate is already nil'd. Beautiful. I also changed foo to be (atomic, strong). I think I'm good!
So in my case, did I even need that weak/strong stuff anyway or should that always be the default? Seems like just lots of extra code.
 
Rob
No, as rmaddy points out, it's not necessary in this case. You generally only do the weakSelf pattern when holding the block in a class property. In this case it is unnecessary. In answer to your Swift question, the same is true. If you needed the weakSelf pattern, Swift simplifies that a bit, but as it's unnecessary in this case, that's a moot point.
BTW, if you want to see an example of (a) when you need weakSelf pattern; and (b) how you do that in Swift (namely via [self unowned], see stackoverflow.com/a/25717424/1271826).
You can see a discussion of [self unowned] in The Swift Programming Language: Automatic Reference Counting.
 
Ah the "block in a class property" property just made it click for me. Because this is a dispatcher block, my class (e.g. self) has no reference to that block. So it's not necessary. I get it! Thanks.
 
Rob
Yep.
 
Beautiful Rob thanks so much
 
Rob
No problem. Anything else before I let you go?
 
4:54 PM
That's it on this topic.
Still chasing now some deadlock related to AudioComponentInstanceNew but that's probably a separate question.
I can't tell if I'm bumping into an iOS bug
 
Rob
Yep, definitely different question. Go ahead and post a new question on that topic after you are still unclear after you finish you diagnosis/research of the issue.
Good luck. Best wishes.
 
Thanks so much Rob! Have a great day.
 
Rob
Bye.
 

last day (15 days later) »