Ios 下面的代码是否存在潜在内存泄漏?

Ios 下面的代码是否存在潜在内存泄漏?,ios,Ios,下面返回的最后两行代码给了我一个潜在的内存泄漏警告。。。。。这是真阳性警告还是假阳性警告?如果为真,我如何修复它?非常感谢你的帮助 -(UIImage*)setMenuImage:(UIImage*)inImage isColor:(Boolean)bColor { int w = inImage.size.width + (_borderDeep * 2); int h = inImage.size.height + (_borderDeep * 2); CGCol

下面返回的最后两行代码给了我一个潜在的内存泄漏警告。。。。。这是真阳性警告还是假阳性警告?如果为真,我如何修复它?非常感谢你的帮助

-(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];
}
您正在泄漏存储在图像变量中的CGImage对象。您可以通过在创建UIImage后释放映像来修复此问题

原因是CoreGraphics遵循CoreFoundation所有权规则;在这种情况下。也就是说,带有Create或Copy的函数返回一个您需要释放自己的对象。因此,在本例中,CGBitmapContextCreateImage返回一个由您负责发布的CGImageRef

顺便提一下,为什么不使用UIGraphics便利函数来创建上下文?这些将处理在结果图像上放置正确的比例。如果要匹配输入图像,也可以这样做

CGSize size = inImage.size;
size.width += _borderDeep*2;
size.height += _borderDeep*2;
UIGraphicsBeginImageContextWithOptions(size, NO, inImage.scale); // could pass YES for opaque if you know it will be
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[inImage drawInRect:(CGRect){{_borderDeep, _borderDeep}, inImage.size}];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
您正在泄漏存储在图像变量中的CGImage对象。您可以通过在创建UIImage后释放映像来修复此问题

原因是CoreGraphics遵循CoreFoundation所有权规则;在这种情况下。也就是说,带有Create或Copy的函数返回一个您需要释放自己的对象。因此,在本例中,CGBitmapContextCreateImage返回一个由您负责发布的CGImageRef

顺便提一下,为什么不使用UIGraphics便利函数来创建上下文?这些将处理在结果图像上放置正确的比例。如果要匹配输入图像,也可以这样做

CGSize size = inImage.size;
size.width += _borderDeep*2;
size.height += _borderDeep*2;
UIGraphicsBeginImageContextWithOptions(size, NO, inImage.scale); // could pass YES for opaque if you know it will be
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[inImage drawInRect:(CGRect){{_borderDeep, _borderDeep}, inImage.size}];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

你必须释放你制作的CGImageRef。CGBitmapContextCreateImage在名称中包含create,这意味着Apple严格遵守其命名约定,您负责释放此内存

将最后一行替换为

UIImage *uiimage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
return uiimage;

你必须释放你制作的CGImageRef。CGBitmapContextCreateImage在名称中包含create,这意味着Apple严格遵守其命名约定,您负责释放此内存

将最后一行替换为

UIImage *uiimage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
return uiimage;

非常感谢你的帮助,凯文!:非常有用的答案!非常感谢你的帮助,凯文!:非常有用的答案!