title: NSMutablearray addobject , added objects not expected
body: <p>I've a question about <code>NSMutablearray</code> <code>addobject</code>, I think it's about object lifecycle. Searched but not got answer. </p>
  
  <p>Here's the code:</p>
  
  <pre><code>    NSMutableString *str1 = [[NSMutableString alloc]init] ;
      NSMutableArray *arr1 = [[NSMutableArray alloc]init] ;
      for (int i=0 ;i&lt;3 ;i++) {
          str1 =[NSMutableString stringWithFormat:@"%ld",(random() % 100) ] ;
          [arr1 addObject:str1] ;
          NSLog(@"arr1 inside loop:%@",arr1) ;
          [str1 setString:@""] ;    }
      NSLog(@"arr1 outside loop:%@",arr1) ;
  
      NSMutableString *str2 = [[NSMutableString alloc]init] ;
      NSMutableArray *arr2 = [[NSMutableArray alloc]init] ;
      for (int i=0 ;i&lt;3 ;i++) {
          str2 =[NSMutableString stringWithFormat:@"%ld",(random() % 100) ] ;
          [arr2 addObject:str2] ;        }
      [str2 setString:@"abc"] ;
      NSLog(@"arr1:%@",arr2) ;
  </code></pre>
  
  <p>I expect that <code>arr1</code> &amp; <code>arr2</code> both include 3 random number objects.
  but the real output is:</p>
  
  <pre><code>arr1 inside loop:(
      83
  )
  arr1 inside loop:(
      "",
      86
  )
  arr1 inside loop:(
      "",
      "",
      77
  )
  arr1 outside loop:(
      "",
      "",
      ""
  )
  arr2:(
      15,
      93,
      abc
  )
  </code></pre>
  
  <p>Q1: <code>Arr1</code>, why the added objects changed to <code>""</code>?
  (If the reason is all 3 objects are just 3 pointers and they point to the same object, then I have Q2 --)</p>
  
  <p>Q2: <code>Arr2</code>, why the last object of <code>arr2</code> is <code>"abc"</code> but the others not.</p>
  
  <p>Thanks! </p>