Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which objects hold references to other objects that are causing memory leaks in objective-c?

I have tried using the leaks tool, and "analyze" etc to find the leak, but it can't find it. Using allocations I can determine the objects which are not being released.

I have noticed (by adding debugging statements in the dealloc method), that dealloc is not called for these objects.

How can I determine which objects are holding references to these objects and preventing them from being released?

like image 491
xcoder Avatar asked Nov 29 '11 12:11

xcoder


People also ask

How do you find memory leaks in Objective C?

Diagnose the Memory Leak Expand “Open Developer Tool,” and select “Instruments” Now choose “Leaks,” and make sure you have chosen your target app and device at the top (“Choose a profiling template for…”):

What is a memory leak Objective C?

A memory leak occurs when the system is unable to determine if allocated space in memory is in use or not. Both Swift and Objective-C use Automatic Reference Counting (ARC) to manage space in memory. ARC is a memory-management system that keeps track of how many references there are to a given object.

Which condition causes memory leak?

Memory leaks are a common error in programming, especially when using languages that have no built in automatic garbage collection, such as C and C++. Typically, a memory leak occurs because dynamically allocated memory has become unreachable.


2 Answers

If you need to see where retains, releases and autoreleases occur for an object use instruments:

Run in instruments, in Allocations set "Record reference counts" on on (you have to stop recording to set the option). Cause the picker to run, stop recording, search for there ivar (datePickerView), drill down and you will be able to see where all retains, releases and autoreleases occurred.

enter image description here

like image 167
zaph Avatar answered Sep 25 '22 07:09

zaph


The analyze tool was unable to detect the problem. Using the allocations tool to capture all the reference counts was a start, but there were so many classes I didn't recognize, or access directly, I was not able to track down the problem using this method. Instead, I made a list of the classes that I was directly responsible for, and investigated each of them line by line till I found the problems. The cause was that I used some third party libraries which didn't decrement the retain count of some of my objects as expected. I guess in this case, following better software engineering principles / design patterns, and having thorough code reviews may have caught the problem earlier.

like image 38
xcoder Avatar answered Sep 23 '22 07:09

xcoder