title: How can I resolve my CoreGraphics Memory Leak without crashing?
body: <p>I have a code iterator that is a test function for conversion of raw data to UIImage objects for file storage. I have two failure paths or options. I can fail:</p>
  
  <p>A.)By having a leak (instruments doesn't show it--instruments all allocations seems to be stable, I have to iterate a number of times say 50 and it will crash running the test function through instruments or normally). Memory appears to be the culprit due to "b" option below. </p>
  
  <p>B.)I can do a forced release of the CG object within UIImage in which case the app will proceed. Once the iterator completes the test will crash on the very last iteration trying to free the CGImage underneath the image returned from my sub-function.  The iterator will complete it's run no matter how many iterations I request. This would point to the fact I have fixed a leak. So now I'm trying to puzzle together what is special about the very last iteration? (ARC?)</p>
  
  <pre><code>- (UIImage *) convertRGBToUIImage:(unsigned char *) buffer 
                              withWidth:(int) width
                             withHeight:(int) height 
  {
  
     CGColorSpaceRef colorSpace       = nil;
     CGContextRef    context          = nil;
     CGImageRef      ref              = nil;
     UIImage        *retImg           = nil;
  
     colorSpace = CGColorSpaceCreateDeviceRGB();
     if (colorSpace == NULL)
     {
        NSLog(@"ERROR: allocating color space convert Bitmap\n");
        return nil;
     }
  
     context = CGBitmapContextCreate (buffer, width, height, 8, width * 4, colorSpace,     kCGImageAlphaPremultipliedLast);
     CGColorSpaceRelease(colorSpace );
  
     if (context == NULL)
     {
       NSLog(@"ERROR: Context not created!");
       return nil;
     }
  
     ref = CGBitmapContextCreateImage(context);                                     
     CGContextRelease(context);
  
     retImg = [UIImage imageWithCGImage:ref];
     CGImageRelease(ref);
  
     return retImg;
  }
  </code></pre>
  
  <p>iterator basics</p>
  
  <pre><code>for(int i = 0; i &lt; numberiterations; i++)
  { 
      UIImage *completedImage = [phelper convertRGBToUIImage:pBuffer withWidth:width withHeight:height]
  
      //...save it out or display it
  
      //Turn on one of the scenarios below
  
      //scenario "A" -- will leak memory (instruments doesn't seem to turn anything up)
      //ARC free
      //completedImage = nil; //this will leak and fail after ~50 iterations
  
      //-or-
  
      //scenario "B" -- will crash on the very last iteration every time but can run x times
      //CGImageRelease([completedImage CGImage];
  
  
  }
  </code></pre>
  
  <p>The analysis tool in Xcode prefers scenario 'A' above. Again I can do this and everything seems great but it will not successfully complete the test. I think this points to ARC. I have tried to figure out the __bridge type casting with no success. I can't seem to get the syntax right and perhaps its not the answer to my problem.  Any hints or ideas would be very appreciated.</p>