自定义UIView在[super dealloc]上抛出EXC_BAD_ACCESS

时间:2010-06-20 16:24:40

标签: iphone objective-c memory-management

我正在开发一个iPhone应用程序。

我有一个继承自 UIView 的课程。它有一个方法来添加两个UILabel和一个UIImageView作为类的子视图。 UILabels和UIImageView是在该方法上创建和销毁的。

当我发布这个自定义类时,它在dealloc方法上的[super dealloc]调用失败。

调试器显示: alt text http://img339.imageshack.us/img339/3157/capturadepantalla201006s.png

与UILabel dealloc有关或从superview中删除它。

如果我评论[super dealloc]没有发生错误。

有什么建议吗?

更新

- (void)dealloc {   
    [super dealloc];
}

- (void)drawView:(ARCoordinate *)coordinate {

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [titleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [titleLabel setTextColor:       [UIColor whiteColor]];
    [titleLabel setTextAlignment:   UITextAlignmentCenter];
    [titleLabel setText:            [coordinate title]];
    [titleLabel sizeToFit];
    [titleLabel setFrame:   CGRectMake(BOX_WIDTH / 2.0 - [titleLabel bounds].size.width / 2.0 - 4.0, 
                                       0, 
                                       [titleLabel bounds].size.width + 8.0, 
                                       [titleLabel bounds].size.height + 8.0)];

    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [subTitleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [subTitleLabel setTextColor:        [UIColor whiteColor]];
    [subTitleLabel setTextAlignment:    UITextAlignmentCenter];
    [subTitleLabel setText:         [coordinate subtitle]];
    [subTitleLabel sizeToFit];
    [subTitleLabel setFrame: CGRectMake(BOX_WIDTH / 2.0 - [subTitleLabel bounds].size.width / 2.0 - 4.0, 
                                        21.0, 
                                        [subTitleLabel bounds].size.width + 8.0,
                                        [subTitleLabel bounds].size.height + 8.0)];

    UIImageView *pointView  = [[UIImageView alloc] initWithFrame:CGRectZero];
    [pointView setImage:    [UIImage imageNamed:@"location.png"]];
    [pointView setFrame:    CGRectMake((int)(BOX_WIDTH / 2.0 - [pointView image].size.width / 2.0), (int)(BOX_HEIGHT / 2.0 - [pointView image].size.height / 2.0), [pointView image].size.width, [pointView image].size.height)];

    [self addSubview:titleLabel];
    [self addSubview:subTitleLabel];
    [self addSubview:pointView];
    [self setBackgroundColor:[UIColor clearColor]];

    [titleLabel release];
    [subTitleLabel release];
    [pointView release];

}

1 个答案:

答案 0 :(得分:5)

从屏幕截图中可以看出,正在发布UILabel子视图时发生了崩溃。听起来你是双重释放它。你的代码是这样做的吗?

label = [[UILabel alloc] init ...];
[self addSubview:label];
[label release];

如果是这样,请确保label方法中发布dealloc,因为对它的唯一引用是它是子视图。如果你打电话给[label release]你是双重释放UILabel,那么当第二个release被发送时(由系统,作为标签的一部分从其超级视图中删除),它已经被释放。