Find where object is retained with ARC Find where object is retained with ARC xcode xcode

Find where object is retained with ARC


To track growth of an application, Heapshot Analysis has proven very effective. It will capture both true leaks and accretion of memory where the allocations are not accounted for by leaks.

You can see all of the retain/release events, and their backtrace, using the Allocations instrument. Hit the little (i) button on the Allocations instrument and turn on "Record reference counts". Turning on "Only track active allocations" reduces the amount of data collected by Instruments, making it snappier (and dead allocations aren't really useful in this context, but can be in others).

With that, you can dive into any allocation (by clicking on the right-arrow in the address field), see all the retain/release events and see exactly where they occurred.

enter image description here


I managed to find the offending retain by doing the following:

  1. Temporarily add -fno-objc-arc to the object class Compiler Flagsto disable ARC for that class.
  2. Temporarily override retain (just call super) and put a breakpoint on it.
  3. Debug and check the call stack each time retain is called.


Last week I was helping some friends debug leaks in their ARC project.Some tips:

1/ Build for Profiling and start Instruments with Leak Detection. Then explore the currently allocated objects, find the object you want (you can sort them by name) and look into its retain/release history. Note that retain count is not very helpful with ARC. You have to check it manually step by step.

Try to comment all the code which could be the source of leak and then uncomment it step by step.

2/ Put a NSLog into your init and into your dealloc to watch when the object is created and destroyed.

3/ Don't look only to property definitions, watch if property setters are implemented manually. I found a problem in my friends' project looking like this:

@property (weak, nonatomic) id<...> delegate;
@interface ... {    id<...> _delegate;}@synthesize delegate = _delegate;- (void)setDelegate(id<...>)delegate {    _delegate = delegate;  //with ARC this retains the object!}