Unable to free path created by CGPathCreateMutable in Swift

I have this simple code in Swift:

override var bounds : CGRect {
  didSet {
    if (bounds != oldValue) {
      var path = CGPathCreateMutable()
      CGPathAddEllipseInRect(path, nil, bounds)
      self.path = path
      CGPathRelease(path)
    }
  }
}

He should draw a circle that fills the layer when the layer changes bounds.

This is ported from my old Objective-C code that works fine:

- (void)setBounds:(CGRect)bounds
{
  if (CGRectEqualToRect(self.bounds, bounds)) return;

  super.bounds = bounds;
  CGMutablePathRef path = CGPathCreateMutable();
  CGPathAddEllipseInRect(path, nil, bounds);
  self.path = path;
  CGPathRelease(path);
}

However, Swift code crashes with some segfault. If I do not let go of the way, this is normal. If I do not install self.path = path, it is also beautiful (although nothing appears, apparently). If both of them are present, it will work.

I really think that the path created CGPathCreateMutable()should be released, though .. and it looks like ObjC. Does anyone know why it doesn't work in Swift? Or did I do something wrong?

Thank!

+4
1

CF Swift. CGPathCreateMutable() CGPath ( CGPathRef) , . WWDC Swift Interoperability In Depth

+8

Source: https://habr.com/ru/post/1544263/


All Articles