Jesse Bunch
Jesse Bunch

Reputation: 6679

LLDB: Show all objects with a pointer to an object in memory

So, at a breakpoint, I have a random object instance. I want to figure out which objects have a pointer to this object. Is there a way to see this in the debugger console? Maybe something that shows me all the objects that have a retain on the object?

Example: I have a NSViewController instance and I want to see all the other objects that hold a pointer this view controller instance. This would be helpful because it would allow me to see the view controller hierarchy that is encapsulating my instance.

Just a crazy thought I had that would really help at times.

Upvotes: 15

Views: 5696

Answers (2)

Greg Parker
Greg Parker

Reputation: 8012

In lldb, use command script import lldb.macosx.heap to install some memory-searching functions. The ptr_refs command should be able to do what you want; use ptr_refs --help to learn more.

Upvotes: 34

Jeremy Massel
Jeremy Massel

Reputation: 2267

Not an efficient solution, or applicable in all cases, but you could encapsulate the object you're looking for in an accessor method on one of your classes, and put a breakpoint inside. By stepping through the end of the accessor method, you can eventually see all the call points.

Alternatively, you can remove the definition of the variable, and the compiler will spit out a ton of errors, each will also be a call to this object.

I'd suggest using ARC if you're not already. Ideally your code wouldn't be messy enough that you wouldn't be able to identify references by reading through the code, ARC can help a little bit in that department

Upvotes: 0

Related Questions