Objective c Dealoc发出警告

Objective c Dealoc发出警告,objective-c,memory-management,warnings,dealloc,scene,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])){ se

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

我创建了我的场景:

+(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
但我一直收到这样的警告:(可能没什么帮助,但我想我会把截图链接起来

我认为这与dealloc有关。如果我在我的场景中评论掉dealloc,我不会得到这个警告。任何帮助都将不胜感激,谢谢

这是我的statemanager切换场景的方法:

-(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];
    }
}

您应该像这样为gameMenuLabel提供一个retain属性

@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部分再次释放该对象。因此,崩溃。

您应该以文本格式将警告添加到您的帖子中,以便其可搜索。屏幕截图中的代码与问题中的代码完全不同。您的屏幕截图显示断点或崩溃,而不是“警告”切换到ARC(如果可能)。Xcode甚至可以为您执行此操作。请查看编辑>重构