2015-01-20 16 views
0

在分析我的應用程序時,Xcode給了我一個警告,顯示他們是存儲到「色彩空間」(下面的代碼的最後一行)對象的潛在泄漏:CoreGraphics在對象自動釋放的次數太多

CGContextSaveGState(context); 
CGContextAddRect(context, rect); 
CGContextClip(context); 
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); 
CGContextRestoreGState(context); 

因此,我添加了以下行:

CFAutorelease(context); 

並再次分析。這一次,我在下面的第二行的警告「對象的自動釋放次數過多(對‘CGPointMake’在一個呼叫中):

case SWShadowDirectionUp: 
    startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect) - 0.5); 
    endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect) - 0.5); 
       break; 

下面是其中使用的色彩空間變量:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGFloat locations[] = { 0.0, 1.0 }; 
NSArray *colors = @[ (__bridge id)self.startColor.CGColor, (__bridge id)self.endColor.CGColor ]; 
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); 
CFAutorelease(colorSpace); 

但是,我不確定究竟發生了什麼問題,或者如何解決它。有沒有人有任何建議?

+0

你還沒有顯示你對'colorSpace'變量的使用。靜態分析器通常會注意範圍末端(對象將泄漏的位置)的泄漏,而不是對象的使用或聲明。 – 2015-01-20 19:14:25

+0

我的錯誤@IanMacDonald,謝謝你指出。我現在編輯我的問題。 – narner 2015-01-20 19:15:32

+0

如果嘗試使用'CGColorSpaceRelease()',警告是否會消失? – Jack 2015-01-20 19:21:33

回答

0

我不確定你在源代碼中做了什麼錯誤,因爲你沒有包括在內整個範圍colorSpace。這是我嘗試過的和靜態的ana lyzer不會抱怨:

- (UIImage *)decompressImage:(UIImage *)image { 
    CGImageRef imageRef = image.CGImage; 
    size_t width = CGImageGetWidth(imageRef); 
    size_t height = CGImageGetHeight(imageRef); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 
               width * 4, colorSpace, 
       kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); 
    CGColorSpaceRelease(colorSpace); 
    if (!context) 
    return image; 

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), imageRef); 
    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 

    UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation]; 
    CGImageRelease(decompressedImageRef); 
    return decompressedImage; 
} 

只要確保你的範圍在年底前對每一個CreateRelease