Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging strategies for over-retain in ARC?

I've got some objects that are passed to a lot of different views and controllers in my application. They're not getting deallocated when I expect them to. Obviously there is an errant strong pointer somewhere, but the surface area of where it could be is very large--these objects are moved into and out of a lot of different data structures.

My usual go-to solution here is Leaks (which reports no cycles) and Allocations (which lists 500+ retain/releases for this object). Is there any way to reduce my search space here?

Ideally there would be a tool that would let me type in a pointer and see all the strong references to the object, and I could probably eyeball the list and find the extra reference in about 60 seconds. In fact, there is such a tool -- the Object Graph instrument -- but it's not available for iOS software.

like image 907
Drew Avatar asked Feb 07 '13 18:02

Drew


People also ask

How do you find the retain count in an arc?

You will need to click on the info descriptor (the 'i' next to Allocation in the left pane) and click on "Record Reference Counts". You can then profile your app and do a search for the specific class you're looking to inspect.

What is retain count in Swift?

Every object in Swift – an instance of a class – has a property called retainCount. When the retain count is greater than zero, the object is kept in memory. When this retain count reaches zero, the object is removed from memory.

What is the retain cycle?

— in order to deallocate an object from memory, its ARC value must be zero. However, when some two object refers each other via strong references, they prevent the compiler from deallocating either object because their ARC value would always be 1. That is called a retain cycle.

What is an over release?

Overrelease happens when an object receives more release messages than retain messages, that is, it is already deallocated when the last release message is sent to it. The result is typically a crash or undefined behavior.


1 Answers

You want the Allocations instrument. To track an individual object type, start the application. You need to create a heapshot at every significant event (I usually create them at points when you've just transitioned to or from a view controller).

Once you've got a heapshot that should have the object you're interested in tracking down, then you should be able to find that object type under the heapshot's disclosure triangle. For each object of that type, you can get a history of what retains and releases have been sent to that object by clicking on the arrow in that object's row.

like image 123
David Doyle Avatar answered Oct 01 '22 11:10

David Doyle