[英]XCode Analyzer potential memory leak on CGContextClip
xcode分析器表示,使用CGContextClip時,潛在的內存泄漏存儲在“路徑”上。 是什么導致此潛在的內存泄漏?
- (UIImage *)imageMaskToEllipseWithBorderWidth:(float)boarderWidth andBorderColor:(UIColor *)borderColor
{
UIGraphicsBeginImageContextRetinaAware( self.size );
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, self.size.width, self.size.height), NULL);
CGContextAddPath(context, path);
CGContextClip(context); // *** Warning is shown here during static analysis ***
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
if( boarderWidth > 0 && borderColor != nil )
{
[borderColor set];
CGContextSetLineWidth(context, 2.0);
CGContextStrokeEllipseInRect(context, CGRectMake(0, 0, self.size.width, self.size.height));
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
據此 :
Core Foundation函數的名稱指示您何時擁有返回的對象:
- 名稱中嵌入“創建”的對象創建函數;
- 在名稱中嵌入了“復制”的對象復制功能。
如果您擁有一個對象,則有責任在完成使用后放棄所有權(使用
CFRelease
)。
編譯器不會自動管理Core Foundation對象的生存期 。 您必須按照Core Foundation內存管理規則的要求調用
CFRetain
和CFRelease
(或相應的特定於類型的變體)。
您在這里創建path
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, self.size.width, self.size.height), NULL);
但您沒有釋放它。 path
泄漏。 因此,您得到了警告。
要解決此問題,只需在使用path
之后添加以下代碼,
CFRelease(path);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.