如何解决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不会起作用。
目前尚不清楚您在问什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。