Errata: page 282 (memory)


#1

After declaring fields for valueLabel, nameLabel, and imageView in the interface (page 281), we proceed to initialize them:

- (id) initWithStyle: (UITableViewCellStyle) style
    reuseIdentifier: (NSString *) reuseIdentifier
{
    ...
    valueLabel = [[UILabel alloc] initWithFrame: CGRectZero];
    [[self contentView] addSubview: valueLabel];
    [valueLabel release];

    nameLabel = [[UILabel alloc] initWithFrame: CGRectZero];
    [[self contentView] addSubview: nameLabel];
    [nameLabel release];

    imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
    [[self contentView] addSubview: imageView];
    [imageView setContentMode: UIViewContentModeCenter];
    [imageView release];
    ...
}

We get references to valueLabel, nameLabel, and imageView via alloc so we own them, which means we need to release them at some point…but not here!

The issue is that we use those now-released references later, in -layoutSubviews.

- (void) layoutSubviews
{
    ...
    CGRect imageFrame = CGRectMake( inset, inset, 40, 40 );
    [imageView setFrame: imageFrame];

    CGRect nameFrame = ...;
    [nameLabel setFrame: nameFrame];

    CGRect valueFrame = ...
    [valueLabel setFrame: valueFrame];
    ...
}

We’re actually not in (immediate) danger of crashing in -layoutSubviews (despite using released references) because those references had their retain counts incremented by -addSubview: back when they were initialized.

FWIW, I ended up creating a -dealloc method and putting the releases there.


#2

This is not an error.

contentView retains our instance variables, so we’re save to release them there.