Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Conceptually I don't worry too much about the actual retain count. I try more to balance my calls. If I take a retain locally in a method then I will (most likely) need to call a release in the same method.</p> <pre><code>... UIView *newView = [[UIView alloc] init]; &lt;---- retain count gets +1 // ... do more stuff [newView release]; newView = nil; &lt;---- retain count gets -1 ... </code></pre> <p>in the above the calls are balanced so we are good.</p> <pre><code>/* * This creates an autoreleased button. Therefore I am not taking a retain on * it so I don't need to release it */ UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //autorelases &lt;---- this comment is redundant as the code states this </code></pre> <p>It works slightly different for ivars as generally you try to balance the calls in the <code>init</code> and <code>dealloc</code> methods and then use properties to do the right thing throughout your class.</p> <pre><code>/* * As we have used sythesized properties the memory management is taken care * for us if we use dot notation or call the setters/getters */ self.startButton = button; OR [self setStartButton:button]; /* * Because this is an ivar and not a local method variable we balance our calls * in the `init` (if we need to, remember ivars are initialized to nil for us) * and `dealloc` methods. */ - (id)initWithButton:(UIButton *)startButton { self = [super init]; if (self) { /* * Notice we don't use self. You should try to access ivar * directly in your init and dealloc methods to avoid side effects */ _startButton = [startButton retain]; } return self; } - (void)dealloc { // Release any other ivars /* * Notice we don't use self. You should try to access ivar * directly in your init and dealloc methods to avoid side effects */ [_startButton release]; [super dealloc]; } // NOTE // I have prefixed my ivars to make it more explicit when I am accessing them // directly as opposed to accessing them through dot notation of normal method // calls </code></pre> <p>Adding the view is not normally our concern as we trust the framework will manage its tasks memory correctly</p> <pre><code>/* * We don't need to worry about this taking a +1 as our code is doing our * management correctly */ [newView addSubview:startButton]; </code></pre> <p><strong>Memory Management Summary</strong></p> <ul> <li>Local method variables - <strong>normally</strong> retain/release calls should be balanced within the method</li> <li>ivars - <strong>normally</strong> retain/release calls should be balanced with <code>retain</code> in the <code>init</code> and <code>release</code> in <code>dealloc</code> and then always use properties throughout the class to manage all other memory tasks.</li> <li>Don't rely on checking the retain count of objects just balance your memory management calls</li> </ul> <p><strong>viewDidUnload</strong></p> <p>Generally speaking you can see <code>viewDidLoad</code> and <code>viewDidUnload</code> as being a matched pair. Anything you create in <code>viewDidLoad</code> is good to be freed in <code>viewDidUnload</code>. You should not release anything in <code>viewDidUnload</code> that can not be easily recreated check the docs for <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html" rel="nofollow">UIViewController</a>.</p> <p>Your view will only unload if it is not the currently visible screen and low memory levels conditions occur.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload