Amazon.com Widgets Memory Question?
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 05:47:41 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  Memory Question?
Pages: [1]   Go Down
Print
Author Topic: Memory Question?  (Read 423 times)
jonthornham
Full Member
***
Posts: 169



WWW Email




« on: November 26, 2009, 03:07:38 PM »

I am trying to figure out if a step is necessary.

If I create an object which has an NSArray object as a property, and I specify a retain in the accessor methods.

Code: (Objective-C)
#import <UIKit/UIKit.h>

@interface CellsViewController : UIViewController <UITableViewDataSource, UITabeViewDelegate>
{
NSArray *computers;
}

@property (nonatomic, retain) NSArray *computers;

@end

When I get to the viewDidLoad method I do the following.

Code: (Objective-C)
- (void)viewDidLoad 
{
    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook", @"Name", @"White", @"Color", nil];
    NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook Pro", @"Name", @"Silver", @"Color", nil];
    NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"iMac", @"Name", @"White", @"Color", nil];
    NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Mac Mini", @"Name", @"White", @"Color", nil];
    NSDictionary *row5 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Mac Pro", @"Name", @"Silver", @"Color", nil];
   
    NSArray *array = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil];
   
    self.computers = array;
   
    [row1 release];
    [row2 release];
    [row3 release];
    [row4 release];
    [row5 release];
    [array release];
}

I am creating a temporary NSArray named array which I use to set the object array.

Code: (Objective-C)
self.computers = array;

When the array object is set the objects in the array and the array itself are retained which is why I can release all of them in the method. My question has to do with the temporary array. Could I do the following or am I missing something?

Code: (Objective-C)
self.computers = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil];

That way I would not have to create or release the array just the NSDictionary's. I understand the first option looks really clean I am just trying to see if I understand.

Thanks,

Jon
   
 
Logged

Jon Thornham
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #1 on: November 26, 2009, 07:05:43 PM »

Jon,

Quote
Code: (Objective-C)
self.computers = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil];
I believe you'll end up with self.computers having a retain count of 2 since you did the alloc yourself (retain count 1) and then you have the retain property (retain count +1) for computers. This would mean a memory leak occurs after you try to release computers.

I would use autorelease on the array.
Code: (Objective-C)
self.computers = [[[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil] autorelease];
or
Code: (Objective-C)
self.computers = [NSArray arrayWithObjects:row1, row2, row3, row4, row5, nil];
Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #2 on: November 27, 2009, 12:04:48 PM »

Thanks, Ryan I see exactly what you're saying.

Take care,

Jon
Logged

Jon Thornham
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.