[英]Releasing path with CGPathRelease causing BAD_ACCESS
I have a CGMutablePathRef property in a subclassed UIImageview. 我在子类UIImageview中具有CGMutablePathRef属性。 When I create a new path and assign it to the property, CGPathRelease causes an error when I call CGPathContainsPoint.
当我创建一个新路径并将其分配给属性时,当我调用CGPathContainsPoint时,CGPathRelease会导致错误。 IF I don't release the path the code works fine but there is a leak.
如果我不释放路径,则代码可以正常工作,但存在泄漏。 How do I properly transfer ownership and release?
如何正确转让和释放所有权?
.h // UIImageView subclass
@property CGMutablePathRef pathHold;
.m
CGMutablePathRef myPath;
myPath = CGPathCreateMutable();
CGRect myRect2 = holderImageView.bounds;
float midX = CGRectGetMidX(myRect2);
float midY = CGRectGetMidY(myRect2);
CGAffineTransform t =
CGAffineTransformConcat(
CGAffineTransformConcat(
CGAffineTransformMakeTranslation(-midX, -midY),
CGAffineTransformMakeScale(holderImageView.pathZone,holderImageView.pathZone)),
CGAffineTransformMakeTranslation(midX, midY));
CGPathAddEllipseInRect(myPath, &t, myRect2);
CGPathCloseSubpath(myPath);
[holderImageView setPathHold:myPath];
CGPathRelease(myPath); // If path not released, works fine but leak.
[self addSubview:holderImageView];
[holderImageView release];
.m
if(CGPathContainsPoint (self.pathHold, NULL, touchLocation, FALSE )) // This Causes error.
You're declaring your property without the (retain) attribute. 您要声明的属性没有(保留)属性。 It means, when you're assigning the path to this property, the runtime only does a pointer assignment and does not -retain your path, thus when you CGPathRelease() the path reference, it gets deallocated.
这意味着,当您将路径分配给该属性时,运行时仅执行指针分配,而不保留路径,因此,当您对路径引用CGPathRelease()进行分配时,它将被释放。 So, you must declare the the property on the placeholder image class like this:
因此,您必须像这样在占位符图像类上声明属性:
@property (retain) id pathHold;
and then assign it like this: 然后像这样分配它:
[holderImageView setPathHold:(id)myPath];
Please note that in the declaration I used 'id' as type. 请注意,在声明中,我使用“ id”作为类型。 That's because almost any CoreGraphics and CoreFoundation type is in fact a valid Objective-C object, so this conversion (toll-free bridging) can be done and the type casting is only needed to avoid compiler warnings.
这是因为几乎所有CoreGraphics和CoreFoundation类型实际上都是有效的Objective-C对象,因此可以进行此转换(免费桥接),并且只需要进行类型转换即可避免编译器警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.