CFMutablePathRef 可以自动释放吗?
我有一个从类方法返回可变 pathRef 的类,如下所示:
+ (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);
}
现在因为最后一行(释放)位于(返回)之后,所以它永远不会被调用,并且我有泄漏。有人有办法让这个 CGPath 自动发布吗?
非常感谢 :)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不能。您可以从“CF”前缀看出该类型属于 Core Foundation,它没有与 Foundation 的 autoRelease 等效的类型。在某些情况下,Foundation 类(如 NSString)和 Core Foundation 类型(如 CFStringRef)之间存在免费桥接,可以让您解决这个问题,但 CFPath 及其可变对应项不能免费桥接到任何 Foundation班级。
调用该方法后,您必须 CFRelease 可变路径。您可能应该根据内存管理约定来命名您的方法,该约定表明调用者拥有返回的值,因此负责在完成后释放它。
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.
我相信你可以这样做:
return ((CGPathRef)[(__bridge id)somePathRef autorelease]);
I believe you can do:
return ((CGPathRef)[(__bridge id)somePathRef autorelease]);