title: ivars and how to properly instantiate their values (iPhone)
body: <p>I have been developing an app for a while and now I have gotten to the "Instruments-Leaks" part.</p>
  
  <p>I remember a thing that puzzled me about ivars a few months back when I was learning. I took the whole thing a bit on faith and just followed the way Apple and others did it. As far as I can read, the accessors generated by the SDK will take care of the memory management.</p>
  
  <p>But how are ivars themselves initialized? </p>
  
  <p>If I have an ivar like this in my interface;</p>
  
  <pre><code>@interface
      {   
      NSArray *results;
      }
  
      @property(nonatomic, retain) NSArray *results;
      @end
  
  @implementation
  @synthesize results;
  </code></pre>
  
  <p>If I during run time try to do this:</p>
  
  <pre><code>[self setResults:allReadyInitializedArray];
  </code></pre>
  
  <p>It will crash, telling me that this result object was not initialized. If I however do this:</p>
  
  <pre><code>self.results = [[NSArray alloc] init]; //Im assigning this property memory, but hasn't the SDK already done that?
  [self setResults:allReadyInitializedArray];
  </code></pre>
  
  <p>it will work but it will apparently leak memory.</p>
  
  <p>I was under the impression that using the generated
  accessors would release the old value before setting the new, meaning
  the above ought to come out with the old value released and the new with a +1 retain count.</p>
  
  <p>Does it specifically have to do with the ivar being of type NSArray/NSMutableArray, I can't recall it has been a problem with other ivars.</p>
  
  <p>The problem has been particular prominent in my XML parser, where I continuously need to set an ivar value, use it, overwrite this value, use the new value etc.</p>
  
  <p><strong>Would someone please help me outline the correct way "of going from": @property() -> @synthesize -> using the ivar -> to dealloc?</strong></p>
  
  <p>I have read the memory management documents, I have tried looking for some in debt documentation that was within my understanding, but it seems that even though I use ivars on a daily basis I don't understand what goes on behind the scenes.</p>