I have a method that returns a CGPath and is generating analyzer warnings. The method is declared in a protocol. Here is an example implementation that is generating the warning:
“Potential leak of an object allocated on line 47 and stored into ‘path'”:
- (CGPathRef)createPathForBounds:(CGRect)bounds key:(NSString *)key;
{
if ([key isEqualToString:OvalColumn])
{
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
return path;
}
return NULL;
}
Here is example usage that is generating the warning, “Incorrect decrement of the reference count of an object that is not owned at this point by the caller”
CGPathRef path = [self.delegate createPathForBounds:bounds key:someKey];
// Use the path to do some drawing
CGRelease(path);
My memory management is correct; I’m passing back a retained CGPath from my protocol method and I’m releasing it in the calling block, so I know the warnings can be ignored, but I’d like to remove them altogether.
Am I missing a naming convention that will make the analyzer happy?
Can functions be defined in protocols?
How will subclassing work?
- (CGPathRef)newPathForBounds:(CGRect)bounds key:(NSString *)keya detailed note on the topic can be found here
alternatively, you could have chosen to use the attribute
cf_returns_retained, but it’s best (imo) to favor naming conventions.