Solved

Objective-C, Property memory leak in iOS App

Posted on 2010-11-15
2
1,496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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