title: objective c setter method memory management
body: <p>I have question about allocating and releasing objects:</p>
  
  <p>if I have code like this:</p>
  
  <pre><code>MyObject *object = [[MyObject alloc] init];
  NSMutableString *string = [[NSMutableString alloc]initWithString:@"bla"];
  object.myString = string;
  NSLog(@"retain count: %d",[object.myString retainCount]); //gives me 2
  [string release];
  NSLog(@"retain count: %d",[object.myString retainCount]); //gives me 1
  </code></pre>
  
  <p>Than I have exactly what I want. I need just one reference and I have retain count 1</p>
  
  <p>but</p>
  
  <p>if I use</p>
  
  <pre><code>object.myString = [[NSMutableString alloc]initWithString:@"bla"];
  </code></pre>
  
  <p>my property look like this:</p>
  
  <pre><code>@property (nonatomic,retain) NSMutableString *myString;
  </code></pre>
  
  <p>one alloc, and one setter method with retain gives me as retain count 2</p>
  
  <p>If I release the object after resignment than the app crashes. I dont know why?</p>
  
  <p>So , do i have to always create an object with a temporary reference, than assign to real reference and release the temp reference like first code?</p>
  
  <p>or is there any other way? </p>