Obj-C,调用者此时不拥有的对象的引用计数的减量不正确?
升级后我突然收到此警告
调用者此时不拥有的对象的引用计数的不正确递减
有什么想法吗?
+ (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);
}
I'm suddenly getting this warning after upgrading
Incorrect decrement of the reference count of an object that is not owned at this point by the caller
Any ideas ?
+ (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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您可以通过 CGColorGetColorSpace 函数获取 CGColorSpaceRef 。
根据“创建/复制”规则,您不拥有该对象的所有权。
所以你不需要用
CGColorSpaceRelease
来释放它。仅释放您显式分配或复制的对象。
这对于 Objective-C 以及 CF 风格的类都有效。
在 Objective-C 中,这意味着对
alloc
的调用或对copy
的调用(当然还有对retain
的显式调用)将需要一个释放。对于 CF 类,如果您使用名称中带有“create”或“copy”的方法获取了一个对象,则需要释放该对象。当然,显式调用
CFRetain
也需要释放。供您参考,甚至在 CGColorGetColorSpace 函数的文档中也提到了这一点,即使“创建/复制”规则只是明确说明了这一点:
这意味着如果您不显式保留该对象,该对象将不会保留在内存中。因此,如果不这样做,则无需释放它。
Yep. You are getting a
CGColorSpaceRef
through theCGColorGetColorSpace
function.According to the 'Create/Copy' rule, you don't have the ownership on that object.
So you don't need to release it, with
CGColorSpaceRelease
.Only release an object you explicitly allocated or copied.
This is valid for Objective-C, as well as for CF style classes.
In Objective-C, it means that a call to
alloc
or a call tocopy
(and of course an explicit call toretain
) will need a release.With CF classes, you need to release if you acquired an object with a method with 'create' or 'copy' in its name. Of course, an explicit call to
CFRetain
will also need a release.For your information, it's even said in the documentation of the
CGColorGetColorSpace
function, even if the 'Create/Copy' rule is just clear about this:Meaning the object won't persist in memory if you don't retain it explicitly. So if you don't, you don't need to release it.