In my iPhone app, I have a "statistics" view that is displayed by the following method - the method itself is called when the user touches a button. This method is in a UINavigationController
- (void) showStatsView {
StatsViewController *statsViewController = [[StatsViewController alloc] initWithNibName:@"Stats" bundle:[NSBundle mainBundle]];
[self pushViewController:statsViewController animated:YES];
[statsViewController release]; // (1)
}
In the statistics view itself, an NSDictionary, "statsDict" is used. It's declared as a property with the following options: (nonatomic, retain).
In my StatsViewController, the viewDidLoad method creates the instance of the NSDictionary like so: MyAppDelegate *appDelegate= (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; stats = [[NSDictionary alloc] initWithContentsOfFile:[appDelegate statsFilePath]];
My dealloc method for StatsViewController looks like:
- (void)dealloc {
/*
dealloc'ing other stuff here which is irrelavent
*/
[statsDict release]; // (2)
[super dealloc];
}
My problem occurs if I attempt to reload the Statistics view after it's been shown once, that is - I press the button which causes showStatsView to fire, which loads the Statistics view (for the first time), and all is currently fine. If I then press a button on the Statistics View to return back to the main menu (which is done via a call to a method which uses [self popToViewController:MainMenuViewController]; ) - from here, if I press the button which loads the Statistics view again (for the second time), my app crashes with EXC_BAD_ACCESS.
If I remove the line marked (1) in showStatsView, then it works fine, as it does if I remove the line marked (2) in dealloc. However, from what I've read, I should release statsDict, as I allocated it, likewise, I should release statsViewController, as I allocated that too. However, if I do both - it crashes! What should I do?
Have I missed some step in my understanding of objective-c memory management?