Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Objective-C, Property memory leak in iOS App

Posted on 2010-11-15
2
Medium Priority
?
1,514 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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