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.
Select all Open in new window
I thought I was releasing it here:
- (void)viewDidUnload {
self.catPiecesArray = nil;
}
- (void)dealloc {
[self.catPiecesArray release];
}
Select all Open in new window
But this leaks!
So I tried this which does not leak:
self.catPiecesArray = [NSArray arrayWithContentsOfFile:filePath];
Select all 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];
Select all 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!