Solved

Objective-C, Property memory leak in iOS App

Posted on 2010-11-15
2
1,475 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:NV9
2 Comments
 
LVL 5

Accepted Solution

by:
mad_mac earned 500 total points
ID: 34144258
From my memory and my understanding.

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

Open in new window


Will give you a reference count of 2 on the object, the alloc and init will return an object with the reference count set to 1, then assuming you have a retain on your property definition this will increase the count to 2.

The arrayWithContentsOfFile works as this returns an auto release reference, so when the current thread ends it will release the object.

The final example works because you are releasing the initial object allocation.

You should be able to validate this by looking at the retainCount property of the object.
0
 

Author Closing Comment

by:NV9
ID: 34151959
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!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now