2

I experienced something odd with UIView animation blocks.

A view never deallocated after I run my animation code. Before calling animation code I set the view property to nil. Then inside the completion block I removed it from superview so after fade out it's gone.

But -dealloc of that faded out and removed view is never called. Then I thought maybe the completion block retains the view and made a weak reference using __weak which worked. Suddenly the view deallocated properly after animation completion.

But in the animation block itself I simply reference the view and set its alpha to 0. No __weak reference and still the view deallocates properly.

Conclusion: iOS cleans up the animation block after animation is done. But it does NOT clean up the completion block. Means: You must use __weak references for everything in the completion block because it sticks around forever.

Is there a magic way to trigger completion block disposal or cleanup so it releases all strong references? Of course I use ARC btw.

openfrog
  • 40,201
  • 65
  • 225
  • 373
  • 1
    I hope you already know that your conclusion is wrong. You might have a retain cycle as Caleb suggests *if* `self` holds a strong reference to the completion block leading to a memory leak. But this doesn't happen if you're using standard `+[UIView animate...` methods. Would only be able to help further if you post the code. – maroux May 26 '13 at 12:53

1 Answers1

1

It sounds like you had inadvertently created a retain cycle by using a strong reference in your block. The view had a strong reference to the block, and the block had a strong reference to the view, and the mutual strong references prevented either one from ever being deallocated. Converting the block's reference to weak broke the cycle, allowing the view (and eventually the block) to be deallocated.

You'll find a good discussion of the possibility of creating retain cycles in block in this SO question: ARC, ivars in Blocks and Reference Cycles via Captured Self.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272