CFMutablePathRef 可以自动释放吗?

发布于 2024-11-28 22:40:30 字数 1246 浏览 0 评论 0原文

我有一个从类方法返回可变 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-12-05 22:40:30

不,不能。您可以从“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.

蓝海 2024-12-05 22:40:30

我相信你可以这样做:

return ((CGPathRef)[(__bridge id)somePathRef autorelease]);

I believe you can do:

return ((CGPathRef)[(__bridge id)somePathRef autorelease]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文