Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C: alloc of object within init of another object (memory management)
    primarykey
    data
    text
    <p>In my <code>.h file</code> I have:</p> <pre><code>NSMutableArray *myArray; @property (nonatomic, retain) NSMutableArray *myArray; </code></pre> <p>My <code>.m file</code> looks basically like this:</p> <pre><code>@synthesize myArray; - (id) init { self = [super init]; if (self != nil) { self.myArray = .... ? // here I want to create an empty array } return self; } - (void) dealloc { [self.myArray release]; [super dealloc]; } </code></pre> <p>What I'm not sure about is what do to in the <code>init</code>.</p> <p>1)</p> <pre><code>self.myArray = [[NSMutableArray alloc] init]; </code></pre> <p>2) </p> <pre><code>NSMutableArray *tmp = [[NSMutableArray alloc] init]; self.myArray = tmp; [tmp release]; </code></pre> <p>Solution 1 doesn't seem right to me, because of my <code>@property (retain)</code> setting I automatically increase the retain counter when setting self.myArray, but additionally I have already a "+1 retain" due to the <code>[NSMutableArray alloc]</code> and then ending up with a retain count of 2 for that object, but only releasing once in the <code>dealloc</code>. Thus the second solution seems more correct to me, even though it is cumbersome.</p> <p>Also am I wondering if <code>self.myArray = ...</code> is actually the same as <code>[self setMyArray:...]</code> and thus does increase the retain count.</p> <p><strong>UPDATE</strong></p> <p>I actually found the answers (and even more details) <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7295-getters-setters-properties-newbie.html" rel="nofollow noreferrer">here</a> in case anyone is interested in reading more.</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