title: Please help me to find s memory leak in this objective-C method (iOS)
body: <p>Ok, I'm really desperate to find this. I'm new in Objective-C and don't have enough experience in pointers managing. I know an important memory leak is here, because executing this code multiple times result in a bunch of memory warnings, but can't find where is it or how to solve it.</p>
  
  <p>The method below gets some data from fixed arrays and create a group of images from it. It's updating a fixed amount of images, but only the first 'n' counts, so the rest are filled with transparent little images (to clear the previous ones).</p>
  
  <p>(I suspect of the UIImages created every execution because the memory is consumed too fast during execution.)</p>
  
  <p>I've already tried to add to the line</p>
  
  <pre><code>UIImage* img = [UIImage imageWithCGImage:image];
  </code></pre>
  
  <p>an autorelease, because I suspected of the images created every time this code is called, and I suppoused that autorelease should be called when routeView[indexAux].image pointed to the new created images. I think that could work, but it eventually ended in "message sent to deallocated instance" error.</p>
  
  <p>Also tried to replace that part with:</p>
  
  <pre><code>UIImage* img = [UIImage imageWithCGImage:image];
  routeView[indexAux].image = [img copy];
  [img release];
  </code></pre>
  
  <p>...so the only existing copy of the deceased img variable exist in routeView[i].image, and were eventually overwrited by the next execution. But ended getting the same memory leak. Any help or hint is welcome. Thanks in advance!</p>
  
  <pre><code>-(void) update:(NSArray*) arr{
      int indexAux = 0;
      for(NSNumber* i in arr){
          int index = [i integerValue];
          CGContextRef context =  CGBitmapContextCreate(nil,
                                                    routeView[indexAux].frame.size.width,
                                                    routeView[indexAux].frame.size.height,
                                                    8,
                                                    4 * routeView[indexAux].frame.size.width,
                                                    CGColorSpaceCreateDeviceRGB(),
                                                    kCGImageAlphaPremultipliedLast);
  
          self.lineColor = [coloresDeSeccion[index] copy];
  
          CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
          CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
          CGContextSetLineWidth(context, 5.0);
  
          for(int i = sections[index]; i &lt;= sections[index + 1]; i++) {
              CLLocation* location = [routes objectAtIndex:i];
              CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView[indexAux]];
              if(i == sections[index]) { //¿Que hacías aquí?
                  CGContextMoveToPoint(context, point.x, routeView[indexAux].frame.size.height - point.y);
              } else {
                  CGContextAddLineToPoint(context, point.x, routeView[indexAux].frame.size.height - point.y);
              }
          }
  
          CGContextStrokePath(context);
  
          CGImageRef image = CGBitmapContextCreateImage(context);
          UIImage* img = [UIImage imageWithCGImage:image];
  
          routeView[indexAux].image = img;
  
          CGContextRelease(context);
          indexAux++;
      }
  
      for(int i = indexAux; i &lt; 15; i++){
          UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
          UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          routeView[indexAux].image = blank;
      }
  }
  </code></pre>