Search code examples
iosmemory-leakscgimageref

iOS - CGImageRef Potential Leak


I have this code:

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *outputimage = [UIImage imageWithCGImage:imageRef
                                           scale:image.scale
                                     orientation:UIImageOrientationUp];

and I get the warning:

Object leaked: object allocated and stored into 'imageRef' is not referenced later in this execution path and has a retain count of +1

But I am using ARC and cannot use release or autoRelease. How to resolve this?


Solution

  • Just add this code

    CGImageRelease(imageRef);
    

    From CGImageCreateWithImageInRect document,

    The resulting image retains a reference to the original image, which means you may release the original image after calling this function.

    So,what you need to do is just call CGImageRelease to make it retain count -1