title: iPhone error, 'Referenced-counted object is use after it is released error' ; memory leak cleanup
body: <p>I am trying to clean up memory leaks and other issues in an existing iPhone app. I am a little new to Objective C, but have some good programming fundamentals and a general understanding of the memory management that is required when dev'ing iphone apps. My question is about the following method below.</p>
  
  <pre><code>  -(NSDate *)formatDate:(id)value{
  NSLog(@"eja: DetailViewController/ formatDate()");
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
  [dateFormatter release];
  
  
  return [dateFormatter dateFromString:value];
  
  
  }
  </code></pre>
  
  <p>It is returning an error reading "<strong>Referenced-counted object is use after it is released</strong>". I see that dateFormatter is being freed before it is returned/used. The issue is of course that if you put the release after the return statement you get a 'Potential leak of an object' error associated with dateFormatter var declaration. </p>
  
  <p>I also tried "autorelease"</p>
  
  <pre><code>return [[dateFormatter dateFromString:value] autorelease];
  </code></pre>
  
  <p>But I then get the error '<strong>Object sent - autorelease too many times</strong>'.</p>
  
  <p>Any words of advice on how to write this properly so the variables are properly managed?</p>