title: NSMutabledata. Why is the retain count 1 higher that I would have expected?
body: <p>I am trying to floss daily, get my exercise, and make sure I maintain a balance between my retains and releases.</p>
  
  <p>Here is what has me puzzled. I have a ivar:</p>
  
  <pre><code>NSMutabledata *_alignmentData 
  </code></pre>
  
  <p>and a synthesized property associated with it:</p>
  
  <pre><code>// .h file
  @property (nonatomic, retain) NSMutableData *alignmentData;
  
  // .m file
  @synthesize alignmentData=_alignmentData;
  </code></pre>
  
  <p>I start to pull data from a server asynchronously:</p>
  
  <pre><code>self.connection = 
  [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
  </code></pre>
  
  <p>And immediately after allocate a data buffer to be used subsequently in the asynchronous callbacks:</p>
  
  <pre><code>// This prints 0. Cool.
  NSLog(@"BEFORE [_alignmentData retainCount] = %d", [_alignmentData retainCount]);
  
  // Create a place to receive the data that will arrive asynchronously:
  self.alignmentData = [NSMutableData data];
  
  // This prints 2. Shouldn't this be 1 ?!?
  NSLog(@" AFTER [_alignmentData retainCount] = %d", [_alignmentData retainCount]);
  </code></pre>
  
  <p>To complicate matters in the first asynchronous callback that fires after the above allocation of self.alignmentData, I inspect the retain count again:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  
      // This prints 1. 
      NSLog(@"[_alignmentData retainCount] = %d", [_alignmentData retainCount]);
      [self.alignmentData setLength:0];
  
  }
  </code></pre>
  
  <p>So, it appears that the retain count rises to 2 from 0 then drops to 1. Can someone explain to me how this is possible? </p>
  
  <p>Note: I've been told not to use retain counts as a debugging aid but that is simply not practical in a non-garbage collected language such as Objective-C.</p>