What is a retain cycle and how do you break it in Obj-C?
Essence
A retain cycle is two (or more) objects holding strong references to each other; neither count reaches zero, dealloc never runs — a leak. ARC does not detect it: it only counts references, it does not search for unreachable graphs.
Classic cases:
- a
delegatedeclared asstrong; - a block stored in a property of the object that captured
self; NSTimerwith a target (the timer retains the target untilinvalidate);- parent ↔ child in a model tree.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep