iphone programming + moving from one view to another leaking memory
I have a class MarshViewController.h and i am using the following code to move from one viewcontroller to another.
#import "ExpertGameController.h"
@interface MarshViewViewController : UIViewController<UIAlertViewDelegate> {
ExpertGameController *nextExpertGame;
}
@property(nonatomic,retain)ExpertGameController *nextExpertGame;
In .m i have synthesized it and i am using the below method. I have released nextExpertGame but still it leaks memory
-(IBAction)expertGame
{
nextExpertGame=[[ExpertGameController alloc]initWithNibName:@"ExpertGameController" bundle:nil];
[self.navigationController pushViewController:nextExpertGame animated:YES];
[nextExpertGame release];
}
Any help开发者_高级运维 is appreciated.
When you allocated the controller you gave it a retain count of 1, when you pushed it to the navigation controller you gave it a retain count of 2 and then when you released it you brought the retain count back down to 1. When you pop that view controller from the navigation controller the retain count will be brought down to 0 and then it will be deallocated from memory.
The simpler way to move from one View to Another is shown in following code:
#import "ExpertGameController.h"
@interface MarshViewViewController : UIViewController<UIAlertViewDelegate> {
//Your declared variables
}
-(IBAction) expertGame {
ExpertGameController *objExpertGameController = [[ExpertGameController alloc] initWithNibName:@"ExpertGameController" bundle:nil];
[self.navigationController pushViewController: objExpertGameController animated:YES];
[objExpertGameController release];
}
Hope this helps!
精彩评论