Find where object is retained with ARC

后端 未结 5 1476
逝去的感伤
逝去的感伤 2020-12-07 12:03

I have an object that is being retained more than necessary (most likely due to a property that is strong instead of weak). Big codebase, so it\'s

5条回答
  •  难免孤独
    2020-12-07 12:46

    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!
    }

提交回复
热议问题