VansFannel
VansFannel

Reputation: 45921

Custom UIView throws a EXC_BAD_ACCESS on [super dealloc]

I'm developing an iPhone application.

I have a class that inherits from UIView. It has a method to add two UILabels and one UIImageView as subviews of the class. The UILabels and UIImageView are created and destroyed on that method.

When I release this custom class it fails on [super dealloc] call on its dealloc method.

The debugger shows this: alt text http://img339.imageshack.us/img339/3157/capturadepantalla201006s.png

There is something related with UILabel dealloc or removing it from superview.

If I comment the [super dealloc] no error occurs.

Any advice?

UPDATE

- (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];

}

Upvotes: 2

Views: 2706

Answers (1)

Jason
Jason

Reputation: 28600

From the screenshot it appears that the crash is occurring when the UILabel subview is being released. It sounds like you are double-releasing it. Is your code doing something like this?

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

If so, make sure not to release label in your dealloc method since the only reference to it is the fact it is a subview. If you call [label release] you are double-releasing the UILabel, so when the second release is sent (by the system, as part of the label being removed from its superview), it has already been released.

Upvotes: 5

Related Questions