cocos2d杀死层 replaceScene不会调用dealloc保留计数2

问题描述

|| 我的问题是,一些神经元被释放了。我的预览结果是,[CCDirector sharedDirector] replaceScene:[Menu node]];或类似的东西为我做的一切。如果我加载新图层,则背景中会保留活动图层。这导致了奇怪的错误。无论如何,在onExit调用时,底部代码的保留计数为2。从未调用Dealloc。删除rootVC可解决此问题。不幸的是我需要它。
#import \"Help.h\"
#import \"Constants.h\"
#import \"MainMenu.h\"


@implementation Help

-(void)showtext:(ccTime)dt
{
    [self unschedule:_cmd];
    txt.hidden = NO;
}

-(void)onExit
{
    cclOG(@\"retain count %i\",[self retainCount]);
    [super onExit];
}

-(id) init
{
    if ( (self = [super init]) ) 
    {
        Nsstring* bgPath;
        Nsstring* button2Path;
        Nsstring* button2Presspath;
        CGRect textFrame;
        int fontsize;
        if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) {
            bgPath = @\"background-ipad.png\";
            button2Path = @\"button2-ipad.png\";
            button2Presspath = @\"button2_press-ipad.png\";
            textFrame = CGRectMake(60,140,920,500);
            fontsize = 24;
        } else {
            bgPath = @\"background.png\";
            button2Path = @\"button2.png\";
            button2Presspath = @\"button2_press.png\";
            textFrame = CGRectMake(10,45,460,217);
            fontsize = [kMenuFontSize intValue];
        }

        Nsstring* helpText = NSLocalizedString(@\"Bla bla bla yada yada yada\",@\"Helptext\");



        rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; // +1 retain to the layer
        txt = [[[UITextView alloc] initWithFrame:textFrame] autorelease];
        txt.text = helpText;
        [txt setEditable:NO];
        [txt setFont:[UIFont fontWithName:@\"Sui Generis\" size:fontsize]];
        [txt setTextColor:[UIColor whiteColor]];
        [txt setBackgroundColor:[UIColor clearColor]];
        [rootVC.view addSubview:txt];
        txt.hidden = YES;
        [self schedule:@selector(showtext:) interval:1];

        self.isTouchEnabled = NO;

        CGSize ss = [[CCDirector sharedDirector] winSize];

        CCSprite* background = [CCSprite spriteWithFile:bgPath];
        background.position = ccp(ss.width*0.5,ss.height*0.5);
        [self addChild:background z:0];

        cclabelTTF* backLabel = [cclabelTTF labelWithString:NSLocalizedString(@\"Back\",@\"back button at help\") fontName:kMenuFont fontSize:fontsize];
        backLabel.position = ccp(ss.width * 0.1,ss.height * 0.1);
        [backLabel setColor:ccBLACK];
        [self addChild:backLabel z:20];
        CcmenuItemImage* backButton = [CcmenuItemImage itemFromnormalImage:button2Path selectedImage:button2Presspath block:^(id sender) {

            [txt removeFromSuperview];
            id trans = [CCTransitionFade transitionWithDuration:1 scene:[MainMenu node]];
            [[CCDirector sharedDirector] replaceScene:trans];
        } ];
        backButton.position = ccp(ss.width * 0.1,ss.height * 0.1);
        Ccmenu* menu = [Ccmenu menuWithItems:backButton,nil];
        [menu setPosition:ccp(0,0)];
        [self addChild:menu z:10];

    }
    return self;
}

-(void)dealloc
{
    cclOG(@\"Help dealloc\");
    [super dealloc];
}

@end
    

解决方法

         不要调用keepCount 这没用。 您需要确保您的保留和释放是平衡的。有时,这意味着确保您已正确拆除可能保留对象的所有依赖状态。 例如,这:
    [self schedule:@selector(showText:) interval:1];
如果它使用以
self
为目标的
NSTimer
,它将保留
self
。既然您的
onExit
被召唤,您是否需要有效地“取消预定”
self
? (我还没有阅读过cocos2d源码来确切了解)。   我如何释放rootVC =   [UIApplication   sharedApplication] .keyWindow.rootViewController;   // +1保留到图层。 [自   版本]不起作用。 这个问题没有任何意义。调用返回对象的方法不应增加保留计数,显然,释放self不会起作用。 目前尚不清楚您在问什么。