tags:

views:

72

answers:

2

This is something I don't understand. Look at this method (copied from http://blog.blackwhale.at/2009/07/uibutton-in-uitableview-footer/)

See that footerView is allocated on the beginning of the code and never released. As far as I understand, the object should be autoreleased or at least released after used on a regular case, but as this method is run automatically by the tables when they are built, where exactly will this view be released...

or will it leak? I have seen Apple samples of code with stuff like that, so I suppose the object is being released somewhere... but where?

// custom view for footer. will be adjusted to default or specified footer height
// Notice: this will work only for one section within the table view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    if(footerView == nil) {
        //allocate the view if it doesn't exist yet
        footerView  = [[UIView alloc] init];

        //we would like to show a gloosy red button, so get the image first
        UIImage *image = [[UIImage imageNamed:@"button_red.png"]
  stretchableImageWithLeftCapWidth:8 topCapHeight:8];

        //create the button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setBackgroundImage:image forState:UIControlStateNormal];

        //the button should be as big as a table view cell
        [button setFrame:CGRectMake(10, 3, 300, 44)];

        //set title, font size and font color
        [button setTitle:@"Remove" forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        //set action of the button
        [button addTarget:self action:@selector(removeAction:)
                        forControlEvents:UIControlEventTouchUpInside];

        //add the button to the view
        [footerView addSubview:button];
    }

    //return the view for the footer
    return footerView;
}

thanks.

+1  A: 

footerView is an instance variable. Is it released in the dealloc of the class?

Lou Franco
+2  A: 

Seeing as footerView is an instance variable on this class, this code is correct. footerView is not autoreleased (by you, anyway) so it will continue to exist as long as you don't release it (because you "own"/retained it by allocating it). The proper place to do that would be in this class's dealloc method.

As long as you do that, this code looks correct to me. :)

Dave DeLong
but suppose that during most part of the time a given class is never deallocated and during the same time, a table is constructed several times. For every time this table is constructed and shown a new footerView will be created, right? So it will be a bunch of leaking footerViews and just the last one will be deallocated when the class is gone, right? (supposing the table is not a class but part of one class)....
Digital Robot
@Digital if you instantiate this class multiple times, then yes you will create multiple footerviews. However, if this class is only instantiated once, then there will only be one footerview. However, that's a bit beside the point (I think). As long as you release the footerview when the class is deallocated, your memory management is correct.
Dave DeLong
ahhh and how can I deallocate the view on dealloc method if that view is created locally by the method? Should I store a reference to use on dealloc? Or just being part of a class will make the view be released automatically?
Digital Robot
@Digital while the view was created inside the method, you're storing it in an instance variable (IOW, you declared `footerView` in the .h file), which means you can just do `[footerView release];` in your `dealloc` method.
Dave DeLong
I tried to put a footerView release on dealloc and it is saying the variable is undeclared... so I have to have a reference...
Digital Robot
As you see, I have it declared as an instance variable... So, I have not declared it on .h, or the new declaration would hide the previous one... right?
Digital Robot