2011-05-31 62 views
1

我的問題是,一些圖層nerver被釋放。我的預覽定罪是,[CCDirector sharedDirector] replaceScene:[Menu node]];或類似爲我做的一切。如果我加載一個新圖層,那麼在後臺保留活動圖層。這導致了怪異的錯誤。無論如何,在onExit調用時,底部的代碼保留計數爲2。 Dealloc永遠不會被調用。刪除rootVC可解決問題。不幸的是我需要它。cocos2d殺死圖層。 replaceScene不調用dealloc。保留計數2

#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 

回答

4

不要叫retainCount

這是沒用的。

您需要確保您的保留和發佈是平衡的。有時候,這意味着確保你已經正確地拆除了任何可能保留你的對象的依賴狀態。

例如,這樣的:

[self schedule:@selector(showText:) interval:1]; 

如果使用NSTimerself作爲目標,它會保留self。由於您的號碼是onExit,您是否需要有效地「不計劃」self? (我沒有閱讀過cocos2d的資料來確切地知道)。


如何釋放rootVC = [UIApplication的 sharedApplication] .keyWindow.rootViewController; // +1保留到圖層。 [self release]不起作用。

這個問題沒有任何意義;調用返回對象的方法不應該增加保留計數,顯然,釋放自己不會起作用。

目前還不清楚你在問什麼。

+0

如何釋放rootVC = [UIApplication sharedApplication] .keyWindow.rootViewController; // +1保留到圖層。 [自我釋放]不起作用。 – zeiteisen 2011-06-01 14:24:28