2

I have this code in non-arc, and I think it has a problem - circle retain (self (UIViewController) has the ownership of complete-block, complete-block has the ownership of self). But when I debug, it run in dealloc (mean that not have circle retain count as I think)

[self dismissViewControllerAnimated:YES completion:^{
    [self goToChatViewController:buddyEntity];
}];

Can someone explain for me, why it not circle retain in this code?

9
  • Using self inside a block will sometimes create a retain cycle. 99% of the time it will not create a retain cycle and is perfectly safe. The key is knowing when it's an issue... for which there is plenty of documentation available. Stack Overflow is not the place to explain that. Without seeing more of your code, I can't tell you if you have a retain cycle or not. Nov 25, 2014 at 8:45
  • I think above code is enough for this question. I think in my mind, self hold completion-block, completion-block hold self. But I don't know "what wrong in my mind", why it not circle retain. Why you say "99% of the time it will not create a retain cycle". I think it is 100% circle retain, but it not correct
    – huynguyen
    Nov 25, 2014 at 8:52
  • What makes you think the code above will create a retain cycle? Nov 25, 2014 at 8:53
  • Because I think "self hold completion-block, completion-block hold self"
    – huynguyen
    Nov 25, 2014 at 8:54
  • Since you're not using ARC nothing holds onto anything. Nov 25, 2014 at 9:05

2 Answers 2

4

You don't hold reference at this block. So this block deallocated after completion. When block deallocating it release object that it hold. So there is all ok. You can courageously use self inside such blocks.

Retain cycles appear for example if you use block as property or add it in container.

3
  • In code below SDK, I think UIViewController have to save completion-block as a property, to call it after dismiss complete. But it is not retain circle. If they don't store completion-block, how it can call block when dismiss complete ?
    – huynguyen
    Nov 25, 2014 at 9:49
  • 1
    @huync, UIViewController will copy block, but it will release it after calling.
    – Cy-4AH
    Nov 25, 2014 at 10:05
  • 1
    Thanks, I think your answer make me understand. Accepted
    – huynguyen
    Nov 25, 2014 at 10:09
0

A block is secretly an ObjectiveC object. When you create a block in the background objectiveC creates an instance of a block class (although quite weirdly it creates it on the stack rather than the heap, unless you copy the block). The objects used inside of your block become instance variables of the block object.

Because the block object is on the heap, not on the stack normally when you get to the end of the method you are in the block object falls out of scope and everything is great. However if your class calls copy on the block, and keeps a reference, the block is copied to the heap. Now your class has a reference to the block and the block has a reference to your class and you have a retain cycle.

9
  • 1
    Every block retain objects that it use. It doesn't matter copy block or not.
    – Cy-4AH
    Nov 25, 2014 at 9:44
  • Blocks on the heap don't increment the retain counts of their instance variables. As they are on the heap they don't need to! This happens when you call copy on them. At this point they become a slightly different type of object (you will notice this in your debugger if you call class on the block object that has been copied) and are move to the heap. After a quick scan this looks like a reasonable explanation of blocks albertodebortoli.github.io/blog/2013/04/21/… Nov 25, 2014 at 10:39
  • Are you sure? You can create pointer at block. Then release all objects that used in that block. Then call this block from pointer. And look we didn't create block's copy, we used block, located at heap. But all works fine, because block retained objects.
    – Cy-4AH
    Nov 25, 2014 at 10:50
  • Shockingly I just wrote the code you suggested and guess what? It crashed twistedape.me.uk/blog/images/blockCrash.png don't forget to turn zombies if you want to copy it. Nov 25, 2014 at 11:16
  • You have EXC_BREAKPOINT exception. It's not EXC_BAD_ACCESS. Something wrong with you project. My output: 2014-11-25 14:50:18.443 GGTest[2732:303] <NSView: 0x100129cd0>, retainCount = 1
    – Cy-4AH
    Nov 25, 2014 at 11:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.