Avatar of NV9
NV9
 asked on

Objective-C, Property memory leak in iOS App

I'm using instruments to check for leaks and I'm running into a leak that I've solved but can't seem to understand the theory behind it. I'm looking for someone to explain it to me. I've read http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html but it's not helping me understand.

Here's the scoop:

I had this line in a viewWillAppear: method.

 
self.catPiecesArray = [[NSArray alloc] initWithContentsOfFile:filePath]; // creates memory leak.

Open in new window


I thought I was releasing it here:

 
- (void)viewDidUnload {
    self.catPiecesArray = nil;
}

- (void)dealloc {
    [self.catPiecesArray release];
}

Open in new window


But this leaks!

So I tried this which does not leak:
 
self.catPiecesArray = [NSArray arrayWithContentsOfFile:filePath];

Open in new window


This made me curious as to whether this would also work— which it does:

 
NSArray *testArr = [[NSArray alloc] initWithContentsOfFile:filePath];
self.catPiecesArray = testArr;
[testArr release];

Open in new window


Like I said I don't understand why the later 2 examples work but my original code does not. Can anyone enlighten me, please. Thanks!
Swift Programming

Avatar of undefined
Last Comment
NV9

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mad_mac

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
NV9

ASKER
Thanks— that was helpful. I NSLogged out the ratainCounts at various points along the flow and that really helped clear the picture. Thanks for your help!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy