views:

55

answers:

1

Hello.

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

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

}
+3  A: 

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.

Jason
I've added the source code. Sorry.
VansFannel
If I remove `[titleLabel release];[subTitleLabel release];`from `drawView`method it works!!
VansFannel
So where are you releasing the memory you are allocating in drawView then? Sounds like a memory leak to me.
Typeoneerror
@Typeonerror: It works if I don't release titleLabel, and subTitleLabel. I don't know if it's a memory leak, but as you can see on the debugger picture, when the UILabel is removed from its superview, it's also released (the reason of my error).
VansFannel