I have a class returning mutable pathRef's from class methods like this:
+ (CGMutablePathRef)roundedRectanglePathInRectangle:(CGRect)rect withCornerRadius:(float)radius{
CGMutablePathRef pathRef = CGPathCreateMutable();
float x = rect.origin.x;
float y = rect.origin.y;
float w = rect.size.width;
float h = rect.size.height;
if (radius > w / 2) {
radius = w / 2;
}
if (radius > h / 2) {
radius = h / 2;
}
CGPoint tl = CGPointMake(x + radius, y + radius);
CGPoint tr = CGPointMake(x + w -radius, y +radius);
CGPoint br = CGPointMake(x + w -radius, y + h -radius);
CGPoint bl = CGPointMake(x + radius, y + h -radius);
CGPathMoveToPoint(pathRef, nil, x, tl.y);
CGPathAddArc(pathRef, nil, tl.x, tl.y, radius, M_PI, 3*M_PI/2, 0);
CGPathAddArc(pathRef, nil, tr.x, tr.y, radius, 3*M_PI/2, 0, 0);
CGPathAddArc(pathRef, nil, br.x, br.y, radius, 0, M_PI/2, 0);
CGPathAddArc(pathRef, nil, bl.x, bl.y, radius, M_PI/2, M_PI, 0);
CGPathAddLineToPoint(pathRef, nil, x, tl.y);
return pathRef;
CGPathRelease(pathRef);
}
now because the last line (release) is after the (return) it is never called and I have a leak. Does anyone have a way to make this CGPath autorelease?
thanks so much :)
No, it can't. You can tell from the "CF" prefix that that type belongs to Core Foundation, which doesn't have an equivalent to Foundation's autoRelease. In some cases, there are toll-free bridges between Foundation classes (like NSString) and Core Foundation types (like CFStringRef) that will let you work around this fact, but CFPath and its mutable counterpart are not toll-free-bridged to any Foundation class.
You'll have to CFRelease your mutable path after you call the method. You should probably name your method according to memory management conventions that indicate that the caller owns the returned value, and is therefore responsible for releasing it when done.