VansFannel
VansFannel

Reputation: 45961

Releasing a view added to another view

I'm developing an iPhone application, with the following method:

- (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.backgroundColor = [UIColor clearColor];
}

This code works well, and compiler doesn't complain about it. But I'm not sure why I haven't had to do the following:

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

I think, it is due to that these views are added to another view (this method is in a class that inherits from View).

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];

Any advice?

Upvotes: 0

Views: 88

Answers (2)

Hollance
Hollance

Reputation: 2976

It depends. Are you doing this on iOS 5 with Automated Reference Counting? Then you no longer need to call release.

If you're on iOS 4 or not using ARC then the compiler won't complain if you don't do the releases, but your code certainly does leak memory.

Try running this code through the Xcode Analyzer, that should point it out to you.

Upvotes: 0

tilo
tilo

Reputation: 14169

In order to avoid memory leaks, you should release these views after adding them as a subview!

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];    

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

Upvotes: 1

Related Questions