The last two lines of code below it’s returning gives me a potential memory leak warning. …..Is this a true positive warning or false positive warning? If true, how do i fix it? Thanks a lot for your help!
-(UIImage*)setMenuImage:(UIImage*)inImage isColor:(Boolean)bColor
{
int w = inImage.size.width + (_borderDeep * 2);
int h = inImage.size.height + (_borderDeep * 2);
CGColorSpaceRef colorSpace;
CGContextRef context;
if (YES == bColor)
{
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
}
else
{
colorSpace = CGColorSpaceCreateDeviceGray();
context = CGBitmapContextCreate(NULL, w, h, 8, w, colorSpace, kCGImageAlphaNone);
}
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(_borderDeep, _borderDeep, inImage.size.width, inImage.size.height), inImage.CGImage);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context); //releasing context
CGColorSpaceRelease(colorSpace); //releasing colorSpace
//// The two lines of code above caused Analyzer gives me a warning of potential leak.....Is this a true positive warning or false positive warning? If true, how do i fix it?
return [UIImage imageWithCGImage:image];
}
You’re leaking the
CGImageobject (that’s stored in yourimagevariable). You can fix this by releasing the image after creating theUIImage.The reason for this is that CoreGraphics follows the CoreFoundation ownership rules; in this case, the “Create” rule. Namely, functions with “Create” (or “Copy”) return an object that you are required to release yourself. So in this case,
CGBitmapContextCreateImage()is returning aCGImageRefthat you are responsible for releasing.Incidentally, why aren’t you using the
UIGraphicsconvenience functions to create your context? Those will handle putting the right scale on the resultingUIImage. If you want to match your input image, you can do that as well