Search code examples
iosmemory-managementmemory-leakscore-foundationcgcontextref

Memory Leak in iOS with CGContextRef


I have a set of code that takes an image stored in a UIImageView and modify's its contents to copy into a new image for a different UIImageView. The problem is that this code always receives a memory warning from my compiler when I analyze the project. I have tried to implement this code in a variety of ways and I always end up receiving a different kind of memory warning. The output from the compiler says that the "Call to function 'CGBitMapContextCreateImage' returns a core foundation object with a +1 retain count", and this causes the image object to have a retain count of +1. If I autorelease the image object the compiler has a memory warning that autorelease is called to many times and the image object originally had a retain count of 0.

Aren't these two compiler warnings contradictory? How can I fix this code as to ensure no memory leaks occur?

-(UIImage *) makeImageLight{

UIImage * image = self.masterImage.image;

NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = width * bytesPerPixel;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = height}, image.CGImage);

UInt8* data = (UInt8*)CGBitmapContextGetData(bmContext);


for (size_t i = 0; i < CGBitmapContextGetWidth(bmContext); i++)
{
    for (size_t j = 0; j < CGBitmapContextGetHeight(bmContext); j++)
    {
        int pixel = j * CGBitmapContextGetWidth(bmContext) + i;

        pixel = pixel * 4;

        UInt8 red = data[pixel + 1];         // If you need this info, enable it
        UInt8 green = data[pixel + 2]; // If you need this info, enable it
        UInt8 blue = data[pixel + 3];    // If you need this info, enable it

        red = ((255 - red) * .3) + red;
        green = ((255 - green) * .3) + green;

        data[pixel + 1] = red;
        data[pixel + 2] = green;
        data[pixel + 3] = blue;

    }
}

// memory warning occurs in the following line:

image = [UIImage imageWithCGImage:CGBitmapContextCreateImage(bmContext)];

CGContextRelease(bmContext);

return image;

}

Nevermind, I fixed it by adding the following code to release the CGImage created from the Context:

CGImageRef imageRef = CGBitmapContextCreateImage(bmContext);

image = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

Solution

  • You use CGBitmapContextCreateImage() to create a CGImage, but you haven't released that CGImage

    You need to split the UIImage creation line as follows:

    CGImageRef cgimage = CGBitmapContextCreateImage(bmContext);
    image = [UIImage imageWithCGImage:cgimage];
    CGImageRelease(cgimage);