Memory Leaks, Properties and Assignment

I’m working on a game that has a game board, that is relatively small array of 4-array objects; a “poor-man’s matrix”. This array, which changes, persists throughout the game view controller, so it’s a property, with the retain attribute. I got the game functional, but recognized a lot of memory leaks when setting up the array.
Given this header file:

@property (nonatomic, retain) NSMutableArray *changingArray;

Basically, I was doing this:
Bad/Wrong way to do it

(void)someMethod{
   myChangingArray = [[NSMutableArray alloc] initWithObjects: obj1, obj2, obj3, nil];
}

Since I know you want to know, this is the right way to do it:

(void)someMethod{
  NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithObjects:obj1, obj2, obj3, nil];
  self.changingArray = tmpArray;
  [tmpMArray release];
}

I have literally read these guidelines about 5 times, from Apple: Practical Memory Management I guess I didn’t really get it, because what sunk in for me was Erica Sadun’s The iPhone Developer’s Cookbook, p. 113 “… for normal (non-autorelease) objects, release the property after assigning it. ” That sounds so antithetical to what you want to do- keep a persistent object throughout the class. What it means is that you create a temporary object and once it’s got the right definition, assign it to the property. Because it is persistent, it already exists and you can’t initialize it. So instead, you create another object and copy it over. Most of the documentation and blog posts out there are about regular, easy to release quick and dirty variables, not retained properties. Hope this helps.

2 Comments »

  1. Comment by Temp666777

    Posted on August 28, 2010 at 4:59 pm

    You make an excellent point .. if you are thinking about memory management, it is essential to bear in mind that :

    PROPERTIES (ie things where you have made it a property by saying “property”) are UTTERLY, TOTALLY, SPECTACULARLY different to normal variables.

    (normal variables of course are nothing more than pointers. they don't even have any memory assinged to them until you use “malloc” or perhaps specify that the variable is going to be such-and-such a type, in which case malloc is handled for you to some extent in most situations.)

    In utter contrast to a variable, a property is more like a squishy “variable” in Perl or something – everything is handled crazily for you behind the scenes.

    You have to be careful when dealing with the two in combination, changing as it were from one to the other, and so on.

    it's well worth remembering too that HOW YOU SETUP your property, intimately affects how it will work, ie those funny words in braces after the property declaration. It's also well worth remembering not to forget, as it were, the “self.” part before the property name — where relevant.

    What a hassle! — Fat Johnnie

  2. Pingback by banane » Blog Archive » A Year in the Trenches- Notes From a Developer on Her App’s 1 Year Anniversary

    Posted on September 10, 2011 at 12:26 pm

    […] NSMutableArrays I do this trick quite frequently where I create a temporary NSMutableArray, manipulate it within my method, then […]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>