Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible scenarios of using alloc/retain/release?
    primarykey
    data
    text
    <p>I have been using ojective c for almost a week, and I am mainly a c++ coder. After I read Apple's memory management guide, I try to bring my memory usage style in c++ into objective c... I tried to conclude these scenarios, I think I won't make memory mistake if I follow these instructions. Please kindly let me know if I am wrong :)</p> <p>I will try not to use autorelease, personally speaking, by using autorelease, there might always be some redundent memory before certain auto release pool is drained. I will only use release, which make sure my application uses minimum memory at any time.</p> <p>Another thing Apple says, which I describe with my own words, is: everytime I add an retain/alloc/copy, I should add a <em>release</em> somewhere.</p> <p>Here are all the scenarios I conclude:</p> <ol> <li><p>In the same function: <em>alloc</em> an object, use it, and <em>release</em> it</p></li> <li><p>In the <em>init</em> function of a class, <em>alloc</em> an object, in the <em>dealloc</em> function of the class, <em>release</em> the object</p></li> <li><p>When it's necessary to own a pointer, one should <em>retain</em> an input pointer in a method of a class(let's say <em>method A</em>), and then <em>release</em> the pointer in the <em>dealloc</em> function of the class. </p> <p>I found that the timing of using <em>retain</em> in objective c is the same to the timing of using <em>memcpy</em> in c/c++, so I take <em>retain</em> as a "memory efficient copy"</p> <p>If the input retained pointer is to set to a member pointer variable, then one should release the member pointer first. So in case[3], <em>alloc</em> in <em>init</em> of the class is paired with <em>release</em> in <em>method A</em>, and the <em>retain</em> in <em>method A</em> is paired with the <em>release</em> in <em>dealloc</em></p></li> <li><p>Return a pointer as return value. Honestly speaking I never do such things when I use c++. It's OK if to return a member pointer, coz someone will take care of it:</p> <pre><code>-(UIButton*) getTheButton() { return theButton; } </code></pre> <p>But it's really terrible to return a pointer to a locally allocated object:</p> <pre><code>-(UIButton*) getTheButton() { UIButton* myButton = [[UIButton alloc] init]; return myButton; //TERRIBLE! } </code></pre> <p>Someone might say I should use autorelease in that case, but I simply want to bypass that solution by using this: I will only return member pointers, or I will not return pointers, I will only operate on given input pointers.</p> <pre><code>-void operateOnTheButton(UIButton* button) { [button release]; button = [[UIButton alloc] init]; } </code></pre></li> </ol> <p>So, please kindly let me know if there is any problem if I follow the memory usage instructions above.</p> <p>Thanks :D</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.
 

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