Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I release this CGPath when I need to return it

I have a method that returns a CGMutablePathRef, something like this:

- (CGMutablePathRef)somePath;  
{  
    CGMutablePathRef theLine = CGPathCreateMutable(); 

    CGPathMoveToPoint(theLine, NULL, 50, 50);  
    CGPathAddLineToPoint(theLine, NULL, 160, 480);  
    CGPathAddLineToPoint(theLine, NULL, 270, 50);

    return theLine;  
}  

The Xcode/Clang static analyzer warns that there's a potential leak. The docs say to call CGPathRelease() but where would I put that?

If I put that before the method returns won't that cause theLine to disappear before it's returned to it's caller?

like image 608
willc2 Avatar asked Nov 22 '09 20:11

willc2


2 Answers

If a method name begins with new, clang will expect it to return an object with a retain count of 1. In other words, renaming the method from somePath to newSomePath will quiet the Analyzer.

If you actually want to autorelease the CGPathRef, you might be able to cast it to an NSObject and then call autorelease. I know that works for CFTypes, I'm honestly not sure if CGPathRef qualifies.

like image 180
benzado Avatar answered Oct 23 '22 03:10

benzado


How about creating a mutablepath, passing it to your building function, then using and releasing it:

CGMutablePathRef mPath = CGPathCreateMutable();
[buildPath:mPath]; //adds reusable lines etc
...do something with it...
CGPathRelease(mPath);
like image 8
Mobs Avatar answered Oct 23 '22 01:10

Mobs