Finding all references to an object instance in Obj-C

后端 未结 3 2087
盖世英雄少女心
盖世英雄少女心 2021-02-13 18:55

I\'m trying to track down some bugs, and one of them is related to a memory leak. It\'s an object that I can tell that something still has a reference to, since Instruments stil

相关标签:
3条回答
  • 2021-02-13 19:36

    I would recommend using the Allocations/ObjectAllocations Instruments template and then in the top right corner type the class name of your object (in the Category field).

    You can then see the allocations increasing as you suggest and by viewing the extended detail you can see where they were allocated.

    All content below this point was added by the OP (joshbuhler)

    In the screenshot below, change the filter to "Objects List", and then by clicking on the little arrow to the right of the object's address, the history of memory events (alloc/retain/release/dealloc) will be show for that object. It won't show you exactly what is hanging onto that object, but it will give you some very useful info for tracking it down.

    enter image description here

    0 讨论(0)
  • 2021-02-13 19:37

    If you're using xCode you can use the Performance tools to find the memory leaks. That will give you a nice graph of ALL memory allocation and if they are released or leaked.

    xcode -> run -> Start with Performance Tools -> Leaks.

    Memory leak detection tools

    0 讨论(0)
  • 2021-02-13 19:50

    Cautionary Tail: :) In the process of searching for a memory leak, I set a breakpoint (really a logpoint) in Xcode that would log the value of self when it was triggered troublesome logpoint image. Meanwhile I found the leak and patched it, but the memory usage wasn't leveling out, and my de-init was never getting called. The logpoint I set earlier was actually causing the retain count of my object to increase, and in turn that prevented de-init from ever getting called. I happened to discover this after several hours of wild goose chases which culminated in me stepping through my object's methods line by line, issuing p CFGetRetainCount(self) from debug console. When I stepped over the line with the logpoint, the retain count went up. At first I assumed it was some strange side effect of my code. I moved that logpoint so that I could set a normal breakpoint on that line, and my problem moved with it. I disabled the logpoint and the leak was gone. Hopefully this can help someone else.

    0 讨论(0)
提交回复
热议问题