title: Objective-C copy an NSArray properly?
body: <p>My iPhone app keeps crashing, and I've narrowed it down to this one line over the past bloody week:</p>
  
  <pre><code>NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&amp;error];
  </code></pre>
  
  <p>I'm getting the right result from the above, but the app crashes after accessing it (<code>EXC_BAD_ACCESS</code>). 
  How do I just copy the contents of <code>fetchResults</code> so that I can play around with it?</p>
  
  <p>I've tried</p>
  
  <pre><code>NSArray *retVal = [[NSArray alloc] initWithArray:fetchResults];
  NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults copy]];
  NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults retain]];
  </code></pre>
  
  <p>but the only thing that doesn't crash the app is</p>
  
  <pre><code>NSArray *retVal = [[NSArray alloc] initWithArray:nil];
  </code></pre>
  
  <p>Could someone help me out? I'm thinking I'll need a lesson in Obj-C memory management.</p>
  
  <p><strong>EDIT:</strong>
  Here's a more complete example of code that crashes:</p>
  
  <pre><code>NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&amp;error];
  [request release];
  NSMutableArray *retVal = [NSMutableArray arrayWithCapacity:0];
  for(Job *job in fetchResults){
      //NSLog(@"dev: %@",job.lastmod_device);
      NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [job.jobkey copy], @"entitykey",
                              [NSNumber numberWithInt:[job.lastmod_device timeIntervalSince1970]], @"job_lastmod_device",
                              [NSNumber numberWithInt:[job.lastmod_server timeIntervalSince1970]], @"job_lastmod_server",
                              [NSNumber numberWithInt:[job.customer.lastmod_device timeIntervalSince1970]], @"customer_lastmod_device",
                              [NSNumber numberWithInt:[job.customer.lastmod_server timeIntervalSince1970]], @"customer_lastmod_server",
                              [NSNumber numberWithInt:[job.productionschedule_lastmod_device timeIntervalSince1970]], @"productionschedule_lastmod_device",
                              [NSNumber numberWithInt:[job.productionschedule_lastmod_server timeIntervalSince1970]], @"productionschedule_lastmod_server", nil];
      //NSLog(@"dict: %@", dict);
      [retVal addObject:dict];
  }
  return retVal;
  </code></pre>
  
  <p>And the code that calls this method:</p>
  
  <pre><code>NSArray *arr2 = [self retrieveJobs];
  </code></pre>
  
  <p>(that's it; I never even use the variable)</p>
  
  <p><strong><em>EDIT 2:</em></strong>
  Even just iterating over the fetched result with an empty for loop and doing nothing else with the <code>fetchResults</code> object makes the app crash. How is this even possible?</p>