1

升级后我突然收到此警告

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

有任何想法吗 ?

+ (void) drawGradientInRect:(CGRect)rect withColors:(NSArray*)colors{

    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors){
        [ar addObject:(id)c.CGColor];
    }


    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);



    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);


    CGContextClipToRect(context, rect);

    CGPoint start = CGPointMake(0.0, 0.0);
    CGPoint end = CGPointMake(0.0, rect.size.height);

    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

    CGColorSpaceRelease(colorSpace);  //on this line
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);

}
4

1 回答 1

5

是的。您正在CGColorSpaceRef通过该CGColorGetColorSpace功能。

根据“创建/复制”规则,您没有该对象的所有权。

所以你不需要释放它,使用CGColorSpaceRelease.

仅释放您明确分配或复制的对象。

这对 Objective-C 以及 CF 样式类都有效。

在 Objective-C 中,这意味着调用alloc或调用copy(当然还有显式调用retain)需要释放。

对于 CF 类,如果您获得了一个名称中带有“create”或“copy”的方法的对象,则需要释放。当然,显式调用CFRetain也需要释放。

供您参考,CGColorGetColorSpace即使“创建/复制”规则对此很清楚,甚至在函数的文档中也说过:

您有责任根据需要保留和释放它。

这意味着如果您不明确保留该对象,则该对象将不会保留在内存中。所以如果你不这样做,你就不需要释放它。

于 2011-11-09T22:11:06.473 回答