-[CALayer release]:消息发送到释放的实例
Posted
技术标签:
【中文标题】-[CALayer release]:消息发送到释放的实例【英文标题】:-[CALayer release]: message sent to deallocated instance 【发布时间】:2011-09-13 12:52:01 【问题描述】:我的一个视图控制器的 loadView: 方法中的一些代码有问题。本质上,我有一个视图,它以更大的视图(在 iPad 上)为中心,并有一些以编程方式加载到其中的标签、按钮和图标。
当视图控制器调用 dealloc 方法并尝试释放时会出现问题。我收到 -[CALayer release]: message sent to deallocated instance 错误并且应用程序崩溃。
通过阅读有关此错误的信息,我似乎过度释放了某些东西,但我认为情况并非如此:下面的代码是麻烦制造者,我已经尝试过自动释放和手动释放调用所有内容,以及根本不释放它们(我知道,我知道,这很糟糕),但它继续造成严重破坏。
container = [[UIView alloc] initWithFrame:CGRectNull];
container.autoresizingMask = ( UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin );
container.frame = CGRectMake( (viewSize.width-320)/2, (viewSize.height-480)/2, 320, 480);
[self.view addSubview:container];
UILabel *welcomeTo = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
welcomeTo.backgroundColor = [UIColor clearColor];
welcomeTo.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18.0];
welcomeTo.frame = CGRectMake(23, 15, 103, 24);
welcomeTo.shadowColor = [UIColor blackColor];
welcomeTo.shadowOffset = CGSizeMake( 0, 1 );
welcomeTo.text = @"Welcome to";
welcomeTo.textAlignment = UITextAlignmentLeft;
welcomeTo.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:welcomeTo];
UILabel *progressions = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
progressions.backgroundColor = [UIColor clearColor];
progressions.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:44.0];
progressions.frame = CGRectMake(20, 30, 280, 56);
progressions.shadowColor = [UIColor blackColor];
progressions.shadowOffset = CGSizeMake( 0, 1 );
progressions.text = @"Progressions";
progressions.textAlignment = UITextAlignmentCenter;
progressions.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:progressions];
UILabel *blurb = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
blurb.backgroundColor = [UIColor clearColor];
blurb.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
blurb.frame = CGRectMake(23, 100, 274, 51);
blurb.numberOfLines = 3;
blurb.shadowColor = [UIColor blackColor];
blurb.shadowOffset = CGSizeMake( 0, 1 );
blurb.text = @"Kick off the fun by adding some content, or if you're not sure what you're doing, check out the \"Getting Started\" guide!";
blurb.textAlignment = UITextAlignmentLeft;
blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:blurb];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 170, 280, 88)];
[button1 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLarge.png"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted];
[button1 addTarget:self action:@selector(createChart:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button1];
[button1 release];
UIImageView *icon1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addchart.png"]];
icon1.frame = CGRectMake(30, 180, icon1.frame.size.width, icon1.frame.size.height);
[container addSubview:icon1];
[icon1 release];
UILabel *button1title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 185, 180, 20)] autorelease];
button1title.backgroundColor = [UIColor clearColor];
button1title.font = [UIFont boldSystemFontOfSize:17.0];
button1title.shadowColor = [UIColor blackColor];
button1title.shadowOffset = CGSizeMake( 0, 1 );
button1title.text = @"Create a Chart";
button1title.textAlignment = UITextAlignmentLeft;
button1title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button1title];
UILabel *button1blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 205, 180, 40)] autorelease];
button1blurb.backgroundColor = [UIColor clearColor];
button1blurb.font = [UIFont systemFontOfSize:13.0];
button1blurb.numberOfLines = 2;
button1blurb.shadowColor = [UIColor blackColor];
button1blurb.shadowOffset = CGSizeMake( 0, 1 );
button1blurb.text = @"A cheat sheet for a displaying a song in any key.";
button1blurb.textAlignment = UITextAlignmentLeft;
button1blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button1blurb];
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(20, 270, 280, 88)];
[button2 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLarge.png"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted];
[button2 addTarget:self action:@selector(createSet:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button2];
[button2 release];
UIImageView *icon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addset.png"]];
icon2.frame = CGRectMake(30, 280, icon2.frame.size.width, icon2.frame.size.height);
[container addSubview:icon2];
[icon2 release];
UILabel *button2title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 285, 180, 20)] autorelease];
button2title.backgroundColor = [UIColor clearColor];
button2title.font = [UIFont boldSystemFontOfSize:17.0];
button2title.shadowColor = [UIColor blackColor];
button2title.shadowOffset = CGSizeMake( 0, 1 );
button2title.text = @"Create a Set";
button2title.textAlignment = UITextAlignmentLeft;
button2title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button2title];
UILabel *button2blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 305, 180, 40)] autorelease];
button2blurb.backgroundColor = [UIColor clearColor];
button2blurb.font = [UIFont systemFontOfSize:13.0];
button2blurb.numberOfLines = 2;
button2blurb.shadowColor = [UIColor blackColor];
button2blurb.shadowOffset = CGSizeMake( 0, 1 );
button2blurb.text = @"A collection of charts that can be displayed like a book.";
button2blurb.textAlignment = UITextAlignmentLeft;
button2blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button2blurb];
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(20, 370, 280, 40)];
[button3 setBackgroundImage:[UIImage imageNamed:@"emptyButtonSmall.png"] forState:UIControlStateNormal];
[button3 setBackgroundImage:[UIImage imageNamed:@"emptyButtonSmallHighlighted.png"] forState:UIControlStateHighlighted];
[button3 addTarget:self action:@selector(gettingStarted:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button3];
[button3 release];
UILabel *button3title = [[[UILabel alloc] initWithFrame:CGRectMake(20, 370, 280, 36)] autorelease];
button3title.backgroundColor = [UIColor clearColor];
button3title.font = [UIFont boldSystemFontOfSize:17.0];
button3title.shadowColor = [UIColor blackColor];
button3title.shadowOffset = CGSizeMake( 0, 1 );
button3title.text = @"Getting Started";
button3title.textAlignment = UITextAlignmentCenter;
button3title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button3title];
UILabel *version = [[[UILabel alloc] initWithFrame:CGRectMake(20, 420, 280, 20)] autorelease];
version.backgroundColor = [UIColor clearColor];
version.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0];
version.shadowColor = [UIColor blackColor];
version.shadowOffset = CGSizeMake( 0, 1 );
version.text = [NSString stringWithFormat:@"Version v%@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber];
version.textAlignment = UITextAlignmentCenter;
version.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:version];
在dealloc方法中释放容器属性,如下:
- (void)dealloc
[leather release];
[backButton release];
[container release];
[super dealloc];
任何人可以提供的答案或建议将不胜感激!我真的被难住了。
【问题讨论】:
你尝试评论这一切并尝试运行吗? 您是否对代码运行过分析?从产品菜单中,选择“分析”。修正所有错误。然后,使用 Instruments -> Leaks 分析应用程序。留意您的代码创建的任何红色泄漏标记(忽略 Apple 库的内容)。 @Nekto - 我做到了。以上代码全部注释掉后就可以正常使用了。 @xj1200 - 分析没有帮助。没有问题。当我分析应用程序时,它也不会导致任何泄漏。 现在逐个取消注释代码块...尝试减少问题代码的数量 【参考方案1】:使用“Zombies”仪器在模拟器中分析您的应用程序。
运行您的应用一段时间,然后尽一切可能使您的应用崩溃。完成后,您会看到如下图所示的弹出窗口,它会停止应用程序的分析:
然后,如果您单击地址旁边的小箭头 (0x158b3c00) .. 它将带您进入已过度释放的对象(僵尸)的对象保留/释放历史记录。
如果您突出显示上面保留计数变为 -1 的行,并打开 View -> Extended detail,它应该将您指向堆栈跟踪和代码中过度释放对象的行:
如果你双击它所在的类,它将打开你的源代码并向你显示错误的代码行:
【讨论】:
+1 了解如何追溯僵尸的解释和图片 很棒的一个..在跟踪方面帮助了我很多。 +1【参考方案2】:过度释放可能发生在对象生命周期内,而不仅仅是在创建对象时。一些提示:
使用访问器确保指针设置为nil
与release
/autorelease
的使用保持一致。就我个人而言,我更喜欢将[[[Class alloc] init] autorelease]
全部放在一条线上。
了解release
/autorelease
之间的区别。 Let's Build NSAutoreleasePool 是一个很好的起点。
【讨论】:
以上是关于-[CALayer release]:消息发送到释放的实例的主要内容,如果未能解决你的问题,请参考以下文章