title: How do i resolve EXC_BAD_ACCESS errors encountered in iphone development
body: <p>I'm trying to do a simple thing; read an image from the internet, save it to the app's documents directory on the iphone, and read it back from that file so that i can do other things with it later.  Writing the file works fine but when i try to read it back i get an EXC_BAD_ACCESS error in GDB that i have no idea how to resolve.  Here is what my code basically looks like:</p>
  
  <pre><code>-(UIImage *) downloadImageToFile {
  NSURL * url = [[NSURL alloc] initWithString: self.urlField.text];
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  
  NSString *documentsDirectory = [paths objectAtIndex:0];
  
  [paths release]
  NSString * path = [documentsDirectory stringByAppendingString:@"/testimg.png"];
  
  NSData * data = [[NSData alloc] initWithContentsOfURL:url];
  
  [data writeToFile:path atomically:YES];
  
  return [[UIImage alloc] initWithContentsOfFile:path];
  }
  </code></pre>
  
  <p>The code fails in the return statement when i try to initialize the UIImage from the file.  Any ideas?</p>
  
  <p><strong>Edit</strong>: neglected to add a release that was the problem in the code originally.</p>