gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:36:02 25 4
gpt4 key购买 nike

我正在开发 iPhone 应用程序。

我有一个继承自UIView的类。它有一种方法可以添加两个 UILabels 和一个 UIImageView 作为该类的 subview 。 UILabels 和 UIImageView 是在该方法上创建和销毁的。

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

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

有一些与 UILabel dealloc 或从 super View 中删除它相关的内容。

如果我注释[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];

}

最佳答案

从屏幕截图来看,崩溃是在释放 UILabel subview 时发生的。听起来你正在双重释放它。你的代码是这样做的吗?

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

如果是这样,请确保不要在您的dealloc方法中释放label,因为对它的唯一引用是它是一个 subview 。如果您调用[label release],您将双重释放UILabel,因此当发送第二个release时(由系统,作为从其标签中删除的标签的一部分) superview),已经发布了。

关于iphone - 自定义 UIView 在 [super dealloc] 上抛出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3079964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com