Reputation: 962
Can someone point me in the right direction. When I load this file in as a nib, and unload it, and reload it instruments says I'm getting a memory leak. Specifically, it's says that where I set the compareOptions NSMutableArray, and where I call [vc release]. CompareOptions is a synthesize property that is also released in the dealloc. Many thanks in advance.
- (void)viewDidLoad{
[super viewDidLoad];
//NSLog(@"Comparison.viewDidLoad");
self.compareOptions = [[NSMutableArray alloc] init];
self.tabs = [[ComparisonTabs alloc] initWithFrame:CGRectMake(450, 85, 650, 50)];
//NSDictionary * currComparison = (NSDictionary*)[data objectAtIndex:0];
//NSArray * correctOptions = [currComparison objectForKey:@"correct"];
for(int i = 0; i < 3; i++)
{
UIViewController * vc = [[UIViewController alloc] initWithNibName:@"ComparisonOptions" bundle:nil];
ComparisonOptions * options = (ComparisonOptions *)vc.view;
[options setup];
options.index = i;
//options.frame = CGRectMake(355 + (306 * i), 475, options.frame.size.width, options.frame.size.height);
//[options setCorrect:[correctOptions objectAtIndex:i]];
[vc release];
[self.view addSubview:options];
[self.compareOptions addObject:options];
}
[self.view addSubview:self.tabs];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabSelectedHandler:) name:@"tabSelected" object:nil ];
[self update:0];}
Upvotes: 0
Views: 121
Reputation: 90117
how does the property of compareOptions and tabs look like? Does it retain your objects?
If it does retain, then you'll get a double retain if you use the setter and alloc.
self.compareOptions = [[NSMutableArray alloc] init];
^ retains ^^^^^ retains
self.tabs = [[ComparisonTabs alloc] initWithFrame:CGRectMake(450, 85, 650, 50)];
^ retains ^^^^^ retains
you could use this instead
self.compareOptions = [NSMutableArray array];
self.tabs = [[[ComparisonTabs alloc] initWithFrame:CGRectMake(450, 85, 650, 50)] autorelease];
UIViewController * vc = [[UIViewController alloc] initWithNibName:@"ComparisonOptions" bundle:nil];
ComparisonOptions * options = (ComparisonOptions *)vc.view;
[vc release];
[self.view addSubview:options];
vc.view (ie options) will be deallocated at the same time vc will be deallocated. And this happens when you call [vc release]. You can't use options after this.
You should release vc after you've added the view to the subview.
And you should think about better class names. I would never assume that ComparisonOptions is a View. It sounds more like NSCaseInsensitiveSearch etc. You know, like it would be an option.
Upvotes: 3