Dealloc发出警告

时间:2012-10-06 21:32:54

标签: objective-c memory-management warnings dealloc scene

我正在练习制作一个简单的应用程序,用场景控制器(状态管理器)类切换场景。

我创建了我的场景:

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];

    GameMenu *layer = [GameMenu node];

    [scene addChild: layer];

    return scene;
}

-(id)init{
    if ((self = [super init])){
        self.isTouchEnabled = YES;
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];
        gameMenuLabel.position = ccp(winSize.width/2, winSize.height/1.5);
        [self addChild:gameMenuLabel];
    }

    return  self;
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"ccTouchesBegan: called from MainMenu object");
    [[StateManager sharedStateManager] runSceneWithID:kGamePlay];

}

-(void)dealloc{
    [gameMenuLabel release];
    gameMenuLabel = nil;

    [super dealloc];
}

@end

但是我一直收到这个警告:https://dl.dropbox.com/u/1885149/Screen%20Shot%202012-10-06%20at%205.23.43%20PM.png(可能没多大帮助,但我想我会链接截图。

我认为这与dealloc有关。如果我在我的场景中注释掉dealloc,我就不会收到此警告。非常感谢任何帮助,谢谢。

这是我的州管理员切换场景的方法:

-(void)runSceneWithID:(SceneTypes)sceneID {
    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;
    switch (sceneID) {
        case kSplashScene:
            sceneToRun = [SplashScene node];
            break;

        case kGameMenu:
            sceneToRun = [GameMenu node];
            break;
        case kGamePlay:
            sceneToRun = [GamePlay node];
            break;
        case kGameOver:
            sceneToRun = [GameOver node];
            break;

        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return;
            break;
    }
    if (sceneToRun == nil) {
        // Revert back, since no new scene was found
        currentScene = oldScene;
        return;
    }    
    if ([[CCDirector sharedDirector] runningScene] == nil) {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
    } else {
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }
}

1 个答案:

答案 0 :(得分:1)

你应该像这样给gameMenuLabel一个保留属性

@property (nonatomic, retain) CCLabelTTF* gameMenuLabel; //in .h file

写下这个......

    self.gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];

而不是......

    gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];

问题是你给gameMenuLabel一个自动释放的对象,然后在dealloc部分再次释放该对象。因此,崩溃。