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);
}
Yep. You are getting a
CGColorSpaceRefthrough theCGColorGetColorSpacefunction.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
allocor 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
CFRetainwill also need a release.For your information, it’s even said in the documentation of the
CGColorGetColorSpacefunction, 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.