Iphone 这里的漏洞在哪里? -(iAction)startGameButtonClicked:(id)发送方{ //gameViewController=NULL; //[gameViewController发布]; //[gameViewController解除锁定]; 如果(!gameViewController){ gameViewController=[[gameViewController alloc]initWithNibName:@“gameViewController”捆绑包:nil]; } appDelegate.ScoreID=0; [gameViewController重置级别]; [游戏视图控制器重置游戏]; [self.navigationController pushViewController:gameViewController动画:是]; }

Iphone 这里的漏洞在哪里? -(iAction)startGameButtonClicked:(id)发送方{ //gameViewController=NULL; //[gameViewController发布]; //[gameViewController解除锁定]; 如果(!gameViewController){ gameViewController=[[gameViewController alloc]initWithNibName:@“gameViewController”捆绑包:nil]; } appDelegate.ScoreID=0; [gameViewController重置级别]; [游戏视图控制器重置游戏]; [self.navigationController pushViewController:gameViewController动画:是]; },iphone,objective-c,xcode,sdk,Iphone,Objective C,Xcode,Sdk,每次单击按钮时,您都会创建一个新的gameViewController并将其推入self.navigationController 您不希望每次都新建一个。将gameViewController设置为.h中的属性 -(IBAction)startGameButtonClicked:(id)sender{ //gameViewController = NULL; //[gameViewController release]; //[gameViewController de

每次单击按钮时,您都会创建一个新的gameViewController并将其推入self.navigationController


您不希望每次都新建一个。

将gameViewController设置为.h中的属性

-(IBAction)startGameButtonClicked:(id)sender{
    //gameViewController = NULL;
    //[gameViewController release];
    //[gameViewController dealloc];

    if(!gameViewController){
        gameViewController = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];
    }


    appDelegate.ScoreID=0;
    [gameViewController resetLevel];
    [gameViewController resetTheGame];
    [self.navigationController pushViewController:gameViewController animated:YES];
} <---Says the leak is here
在.m

@property(nonatomic,retain) GameViewController *gameViewController;
然后在指定时使用该属性

@synthesize gameViewController
最后释放

self.gameViewController = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];

现在它只是突出显示最后一行作为泄漏[gameViewController版本];你以后会释放gameViewController吗?以及resetLevel和resetTheGame方法是否干净?将
gameViewController
制作一个带有属性的ivar(实例变量),并将其引用为
self.gameViewController
[self.navigationController pushViewController:gameViewController animated:YES];
[gameViewController release];