Release?


#1

in HomepwnerAppDelegate.m in method
applicationdidFinishLaunchingWithOptions

We set the itemsViewController possesions array to the array that was unarchived

NSMutableArray *possessionArray = [NSKeyedUnarchiver unarchiveObjectWithFile:possessionPath];

[itemsViewController setPossessions:possessionArray];

I notice we did not release possessionsArray after giving it to possessions.

itemsViewControllerSince setPossession: will use are setter that was created with our @property and @synthesize statements
using the retain keyword.

     @property (readwrite, retain) NSMutableArray *possessions;

I thought that since we used retain in the property the setter method would retain the passes in array?

Thanks


#2

Remember the memory management rules: if a method returns an object and it doesn’t say new, alloc or copy, the object is autoreleased.

Therefore, possessionArray, when created, has a retain count of one and is autoreleased. It is sent to itemsViewController, who retains it (bumping retain count to 2). When application:didFinishLaunchingWithOptions: ends, possessionArray is released because it is in the autorelease pool and itemsViewController is the only owner.

Of course, there is a slight error in another place here that I just realized: the first time the app launches, there is no file at possessionPath and a new NSMutableArray is allocated and given to the itemsViewController via setPossessions:. This one would be a leak and should be autoreleased: (possessionArray = [[[NSMutableArray alloc] init] autorelease];, or possessionArray = [NSMutableArray array];). That will be updated in the next printing.

But, since the possessionArray lives the entire time the application lives, its not truly a leak. But that is confusing so pretend I didn’t say that. :slight_smile:


#3

Great.
Thanks


#4

[quote=“JoeConway”]Of course, there is a slight error in another place here that I just realized: the first time the app launches, there is no file at possessionPath and a new NSMutableArray is allocated and given to the itemsViewController via setPossessions:. This one would be a leak and should be autoreleased: (possessionArray = [[[NSMutableArray alloc] init] autorelease];, or possessionArray = [NSMutableArray array];). That will be updated in the next printing.

But, since the possessionArray lives the entire time the application lives, its not truly a leak. But that is confusing so pretend I didn’t say that. :slight_smile:[/quote]

I don’t think there is a leak there. The code in the book differs from that which you’ve just quoted…

[code]// Unarchive it into an array
NSMutableArray *possessionArray = [NSKeyedUnarchiver unarchiveObjectWithFile:possessionPath];

// If the file did not exist, our possession array will not either
// Create one in its absence
if(!possessionArray)
possessionArray = [NSMutableArray array];[/code]

Looks to me like the array being fed to possessionArray is being allocated by a class method of the NSMutableArray class, so presumably it will be in the autorelease pool.


#5

Oh, wait, next printing? Yeah, I’ll just be hiding in the corner pretending I’m not there… :blush: