question about release and autorelease

Discussion in 'iOS Programming' started by MACloop, Mar 24, 2010.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    Hello,

    I am a bit confused about when to use release or autorelease. If I create an UIButton object like this

    Code:
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor blackColor];
    button.frame= CGRectMake(160, 385, 130, 25);
    button.titleLabel.font = [UIFont boldSystemFontOfSize: 12];
    [button setTitle:@"title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
    [myScrollView addSubview:button];
    
    it will be autoreleased. But, if I add the button to a subview would it be better to alloc the object and then right after adding it to the subview, release it?
    Like this:
    Code:
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(25, 385, 130, 25)];
    button.buttonType = UIButtonTypeCustom;
    button.backgroundColor = [UIColor blackColor];
    button.titleLabel.font = [UIFont boldSystemFontOfSize: 12];
    [button setTitle:@"title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
    [myScrollView addSubview:button];
    [button release];
    
    The autorelease will happend some where in time... but using the release assures the memory to be freed at that very moment?

    Any ideas?
    MACloop
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    release is roughly equivalent to "decrement the retain counter NOW" whereas autorelease is more akin "decrement the retain counter once I'm done with this section". In both cases, nothing is actually deallocated unless the retain count is 0.

    In your first code block, the buttonWithType: method call creates an autoreleased object with a retain count of 1. This means that, once you leave the scope, the retain count will be decremented to 0 and the object will then be deallocated (eventually). In the second code block, the initWithFrame: method call creates an object with a retain count of 1. In both cases, adding the subview sends a retain message to the button object and a release when it is no longer needs tit. In the first case you end up with:

    Code:
    Retain Count - Action
    1 - buttonWithType:
    2 - addSubview:
    1 - autorelease
    0 - button removed from view sometime in the future
    In your second block you end up with:
    Code:
    Retain Count - Action
    1 - initWithFrame:
    2 - addSubview:
    1 - release
    0 - button removed from view sometime in the future
    If you omit the release from the second block, you get:
    Code:
    Retain Count - Action
    1 - initWithFrame:
    2 - addSubview:
    1 - button removed from view (uh oh)
    That last line means you now have an object hanging around with a retain count of 1 that no one knows about or, in other terms, you are leaking memory.
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    Thanks alot for your comment and excellent explanation! It almost seems to be like I supposed it to be. I am abit curoius about whast you wrote about "button removed from view". Is that something I have to do myself or do you mean that when the view is dealloced, the button object will be removed aswell because it now is "a part of" the view self...?
    MACloop
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    UIButton is a little different from other view classes. You should use the UIButton buttonWithType: factory method to create buttons. This forces you to use the first block of code that you show.

    It's not possible to set the buttonType property after a button is created. This is a read-only property.

    Other view classes are correctly created using initWithFrame and can be created using code similar to the code you show in the second block of code.
     
  5. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    Thanks for the comment! Yes, I read about the +buttonWithType: method in the documentation but I was not sure if you have to use this method to create a button... I will follow your advice and go for the first alternative!
    Thanks!
    MACloop
     

Share This Page