游戏中心内存释放(使用Cocos2D)

时间:2011-12-14 14:27:22

标签: memory cocos2d-iphone center memory-leaks

我们刚刚为我们的Cocos 2D游戏添加了Game Kit。它工作正常,但会导致一些严重的内存泄漏。我在这个论坛(以及其他论坛)上阅读了有关它的所有帖子,但似乎没有任何帮助。我们正在使用GKTapper的GameCenterManager。

这就是我们创建排行榜并展示它的方式:

-(void)showLeaderBoard:(NSString *)lb withScore:(int)score
{
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];

if (leaderboardController != nil){
gcVC = [[UIViewController alloc] init];
leaderboardController.category = lb;
leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview: gcVC.view];
[gcVC presentModalViewController:leaderboardController animated:YES];
}

}

此处已删除:

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[gcVC dismissModalViewControllerAnimated:YES];
[gcVC.view.superview removeFromSuperview];
[gcVC release];
}

如果您查看活动监视器中使用的Real Mem,则泄漏很明显。第一次显示并解散了排行榜,应用程序的内存使用量增加了11.42MB,然后每次显示和解除排行榜时它增加约0.3MB。此外,每次你在游戏中心领导板上的“今天”,“本周”和“所有时间”之间浏览时,另外约有0.2MB被吃掉。

1 个答案:

答案 0 :(得分:0)

我相信代码是旧的,并且在cocos2d添加了自己的RootViewController之前使用过。这是我在KKGameKitHelper中使用的代码:

-(UIViewController*) getRootViewController
{
    return (UIViewController*)((KKAppDelegate*)[UIApplication sharedApplication].delegate).rootViewController;
}

-(void) presentViewController:(UIViewController*)vc
{
    UIViewController* rootVC = [self getRootViewController];
    [rootVC presentModalViewController:vc animated:YES];
}

-(void) dismissModalViewController
{
    UIViewController* rootVC = [self getRootViewController];
    [rootVC dismissModalViewControllerAnimated:YES];
}

// Leaderboards

-(void) showLeaderboard
{
    if (isGameCenterAvailable == NO)
        return;

    GKLeaderboardViewController* leaderboardVC = [[[GKLeaderboardViewController alloc] init] autorelease];
    if (leaderboardVC != nil)
    {
        leaderboardVC.leaderboardDelegate = self;
        [self presentViewController:leaderboardVC];
    }
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)viewController
{
    [self dismissModalViewController];
    if ([delegate respondsToSelector:@selector(onLeaderboardViewDismissed)])
    {
        [delegate onLeaderboardViewDismissed];
    }
}