Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode debugging / Instruments: See all pointers to an object

I just started working on a relatively complex project, and have discovered a bug. When the user logs out, the view controllers are still allocated behind the login view controller. They continue responding to rotation events, etc. I have set the controller to nil upon logout, but it's still responding, which indicates that some other object still has a pointer to it. (This project has ARC enabled.)

Pre-ARC I could likely solve this by overriding retain:

- (id) retain
{
    // Break here to see who is retaining me.
    return [super retain];
}

How can I use the Xcode debugging tools to select an object and list all the other objects that point to it? Is there a better approach than simply hunting through all the code?

like image 749
Aaron Brager Avatar asked Jan 29 '13 16:01

Aaron Brager


2 Answers

As of Xcode 8 you can use the Debug Memory Graph:

Run your app and look at the navigator at the bottom of the screen. You should see a set of three connected circles:

enter image description here

Navigate to wherever so that your object is allocated in memory and then tap on this icon. It should pause your app and a graph should pop up on your Xcode screen. Now, on the left side of the screen you can see a list of objects. Find the object you want to see all the pointers to and click on it. Now they should be visible in the graph. You can expand these branches by tapping on the two arrows on the nodes at the left end of the branch. The graph should look something like this:

enter image description here

This saved me literally days of work. It's an extremely useful tool. I hope it helps someone else too.

like image 142
Minimi Avatar answered Oct 22 '22 04:10

Minimi


The Instruments Heapshot Analysis tool was the best thing I could find for this purpose. This article provides a more complete tutorial, but the basic steps are:

  1. Select Product -> Profile. Choose the Allocations Instrument.
  2. On the left, press the Mark Heap button before/after significant events. In my case, this was before login, after login, and after logout.
  3. Search through the Heapshot for the class you're looking for.
  4. Press the disclosure triangle to see its memory address(es).
  5. Press the arrow to the right of a memory address to see its responsible caller (it appears in the rightmost column.)

If someone else writes up a more thorough explanation, or can offer any related lldb commands, I'm happy to mark your answer as correct.

like image 30
Aaron Brager Avatar answered Oct 22 '22 03:10

Aaron Brager