Solved

Objective-C, Property memory leak in iOS App

Posted on 2010-11-15
2
1,506 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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:   …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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