Four Most Likely Sources of a Retain Cycle

Retain cycles are still very much a part of life under ARC.

Here are the four most likely culprits:

  1. NSTimer: If you create an NSTimer on a UIViewController, make sure that you call “invalidate” on the timer when you’re doing using it, otherwise it will retain “self”.

  2. Observers/Notification Center: If you add an observer to the NSNotificationCenter, make sure you remove the observers when you’re doing using them, otherwise references may leak.

  3. Blocks: Closures in iOS can capture strong references in (very) unexpected ways, leading to potentially (very) subtle memory leaks.

    Most iOS programmers know to make weak references to “self” in a closure block, but closures can capture other (actually, any) strong variables as well. I find it a good practice to make a weak reference to any potentially strongly referenced variable that may find its way into a closure.

    So instead of writing:

    // BAD
    void (^myClosure)(void) = ^{
      [leakingRef doSomething];
    };
    

    Under ARC you might try writing something more like:

    // GOOD
    __weak typeof(leakingRef) weakRef = leakingRef;
    void (^myClosure)(void) = ^{
      [weakRef doSomething];
    };
    

    If using manual memory management, you might try writing something like:

    // GOOD
    __block typeof(leakingRef) blockRef = leakingRef;
    void (^myClosure)(void) = ^{
      [blockRef doSomething];
    };
    

  4. Delegates: When setting a delegate property on an object, it’s (usually) best to make it a “weak” reference:

    @property (nonatomic, weak) id<MyProtocol> delegate;
    

To check that the object is being deallocated, implement a simple “dealloc” method with a logging statement and be sure that the statement is being called.

More information here: Debugging Retain Cycles

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s