I added some logging to all of my inits and deallocs to try to understand why my ARC-enabled project was consuming more and more memory as runtime continued. I found that one view controller was responsible. It’s main duty at initialization is to create an NSMutableArray and fill it with NSNull objects. During execution, it swaps out these null objects for more meaningful objects, and vice versa – a basic lazy loading setup.
When this view controller is popped, any non-null views that are in the NSMutableArray at that time are not released. As a result, the view controller itself is also not released.
I can resolve this by either emptying the NSMutableArray or setting it to nil.
What makes this necessary? Is it always my responsibility to set an array to nil if I created it, even under ARC? Or must something else be retaining one of the objects in the array, and preventing it from automatically deallocating?
Assuming the
NSMutableArrayis an instance variable, it should be released when the view controller is released. If your controller’sdeallocis getting called when theNSMutableArrayonly hasNSNullobjects, but it’s not getting deallocated when you fill your array with “meaningful” content, that means that you undoubtedly have a strong reference cycle, i.e. there must be some reference to the view controller, itself, in the contents of theNSMutableArray.