title: iOS - XCode 4.4 - Potential Memory Leaks Using Analyze
body: <p>I ran analyze on my 1st iPhone app, and I see a few potential memory leaks. The app itself works fine on the simulator. 
  I would like to do the right thing and clear the potential memory leaks, but some are quite puzzling. Maybe someone could help me here? </p>
  
  <p>Thanks in advance. </p>
  
  <p>Pier. </p>
  
  <p><strong>Error 1)</strong> The Analyzer says "Potential leak of an object stored in tempDate and tempAns"</p>
  
  <pre><code>#import "Answer.h"
  
  @implementation Answer
  @synthesize answerTiming; 
  @synthesize xPosition; 
  @synthesize earlyOrLate; 
  @synthesize hit; 
  
  + (Answer *) createAnswerWithTiming :(NSDate *)paramTiming andXPosition :(float) xPosition
  {
  NSDate * tempDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:paramTiming];
  Answer * tempAns = [[Answer alloc] init ];
  [tempAns setAnswerTiming:tempDate];
  [tempDate release];
  [tempAns setXPosition:xPosition]; 
  [tempAns setEarlyOrLate:0];
  [tempAns setHit:false];
  
  return tempAns; 
  }
  
  
  - (void)dealloc {
  [answerTiming release];
  [self release];
  [super dealloc]; 
  }
  
  @end
  </code></pre>
  
  <p><strong>Error 2)</strong> Analyzer says (see below) </p>
  
  <pre><code>- (void)viewDidLoad
  {
  [super viewDidLoad];
  
     ........
     ...
  
  UIImage * perfectImage = [UIImage imageNamed: @"perfect.png"];
  self.perfectImageView2 = [[UIImageView alloc]initWithImage:perfectImage]; 
  </code></pre>
  
  <p>// method returns an objective C content with a +1 retain count</p>
  
  <pre><code>[self.perfectImageView2 setFrame:CGRectMake(145.0f,
                                              150.0f,
                                              self.perfectImageView2.image.size.width,
                                              self.perfectImageView2.image.size.height)];
  
  self.view.backgroundColor = [UIColor whiteColor];
  
  UIImage * levelUpImage = [UIImage imageNamed:@"levelup.png"];
  self.levelUpImageView = [[UIImageView alloc] initWithImage:levelUpImage];
  [self.levelUpImageView setFrame:CGRectMake(100.0f,
                                              400.0f,
                                              self.levelUpImageView.image.size.width,
                                              self.levelUpImageView.image.size.height)];
  </code></pre>
  
  <p>//object leaked, allocated object is not referenced later in this execution path and has a retain count of +1</p>
  
  <pre><code>self.view.backgroundColor = [UIColor whiteColor];
  }
  </code></pre>
  
  <p><strong>Error 3)</strong> </p>
  
  <pre><code>- (NSMutableArray *) generateQuestionTapAnswers:(NSString *) answersString withFirstBeat:    (NSDate *) firstBeatTime
  {
  NSArray * notesToDraw = [answersString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" "]];
  
  float noteValueOffset = 0.0; 
  
  NSMutableArray * answerArray = [[NSMutableArray alloc] init ];
  </code></pre>
  
  <p>// Method returns an objective C object with a +1 retain count</p>
  
  <pre><code>for (int i=1; i &lt; notesToDraw.count; i++) // i = 0 is the time signature
  {
   .....
  }
  return answerArray;
  </code></pre>
  
  <p>// Object returned to caller as an owning reference (single retain count transferred to caller)
  // Object leaked: Object allocated and stored into answerArray is returned from a method whose name generateQuestionTapAnswers does not start with copy, mutableCopy
      }</p>