Reputation: 2343
i used the instruments to find leaks in my iphone app and i found that i have leak in this line in my code
tableViewController.dataSource = [[NSMutableArray alloc] initWithArray:[subjects_dic allKeys]];
the property dataSource defined as retain. is this a mistake ?!
Upvotes: 0
Views: 368
Reputation: 4170
Another alternative:
// mutableCopy implicitly retains the array returned by allKeys
NSMutableArray *mutArray = [[subjects_dic allKeys] mutableCopy];
[tableViewController setDataSource:mutArray]; // @property dataSource retains mutArray
[mutArray release];
Upvotes: 1
Reputation: 58458
If you specify retain in your property declaration, then anything you assign to that property will be retained.
So in your example, you have two options:
Instead of creating a new array with the alloc/init approach, you can simply use [NSMutableArray arrayWithArray:[subjects_dic allKeys]];
Release the property once after setting it. This option isn't as good an idea, as it may cause a crash if the property's memory management is changed in the future and this release is forgotten about.
I'd recommend option 1.
Upvotes: 2
Reputation: 6856
Break it up:
NSMutableArray *mutArray = [[NSMutableArray alloc] initWithArray:[subjects_dic allKeys]];
[tableViewController setDataSource:mutArray];
[mutArray release];
It's the same pattern you use for creating, pushing, and releasing Views from the Navigation Controller.
Upvotes: 4