Reputation: 4833
There is undoubtedly a wealth of information re: memory management in iOS. Having read a huge amount about it, I am still unclear as to 'BEST' practice for certain situations. Please can I seek clarification on two examples below...
I have an NSMutableArray which is acting as a datasource for a tableView and a UIBarButtonItem called editButton both declared as follows:
@interface MyTableViewController : UITableViewController {
NSMutableArray *datasourceArray;
UIBarButtonItem *editButton;
}
@property (nonatomic, retain) NSMutableArray *datasourceArray;
@property (nonatomic, retain) UIBarButtonItem *editButton;
@end
I have then synthesized them and alloc'd/init'd them as follows:
@implementation
@syntesize datasourceArray, editButton;
-(void)viewDidLoad {
self.datasourceArray = [self retrieveDatasourceArray];
self.editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO];
[editButton release];
}
-(void)retrieveDatasourceArray {
NSMutableArray *datasource = [[[NSMutableArray alloc] initWithObjects @"example1", @"example2", nil] autorelease];
return datasource;
}
-(void)dealloc {
[datasourceArray release];
[editButton release];
[super dealloc];
}
Question 1: The NSMutableArray
As you can see, I have separated the actual creation of the array into a different method as there is lots of code retrieving from core data and sorting going on (not needed for this question) which I wanted to separate out. I have therefore chosen to return an NSMutableArray which is autoreleased and set this to the self.datasourceArray which is defined in the header file. Is this a sensible and leak free way of implementing this?
Question 2: The Edit Button
As I need to change the title and style of the editButton later, I need to have access to it, hence declaring it. I then alloc/init it in the viewDidLoad method and added it to an array (with some other buttons not shown here) before using this array to add the buttons to the navigationBar. I have then released the editButton as I have alloc'd it and handed it to an array. Is this necessary or essential or even in the correct place given my dealloc method?
Many thanks in advance
EDIT: Further question 3:
When accessing either of these ivars elsewhere in my code (say when calling [datasourceArray count] or resetting the title of the 'Edit' button to 'Done', should I use self. notation or not?
EDIT: Further question 4:
Elsewhere I have used the following code to initialise a synthesised NSMutableArray. Given the below answers, is this more leaky...?
[self setDatasourceArray: [[NSMutableArray arrayWithArray: [self retrieveDatasourceArray]];
Upvotes: 0
Views: 105
Reputation: 21805
1st point for the Array
NSMutableArray *datasource = [[[NSMutableArray alloc] initWithObjects @"example1", @"example2", nil] autorelease];
return datasource;
here you are doing it correct..returning an autoreleased object..which will be retained by the variable because you defined it to be of type retain (when you did @property
).
2nd point for the edit button
self.editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO];
[editButton release];
Here you are apparently over releasing the object.. remember the variable retains the new variable you defined..so edit button retains the new bar button item..so releasing it . is necessary one time..which you do in dealloc..but releasing here also will lead to overrelease..to solve this just remove the release line and update your code to be like this
self.editButton = [[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)]autorelease];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO];
Here you see that the new instance which will be created will be auto released..and its value will be retained by your variable
Upvotes: 1