释放 CGMutablePathRef 是有问题的
Posted
技术标签:
【中文标题】释放 CGMutablePathRef 是有问题的【英文标题】:Releasing CGMutablePathRef is problematic 【发布时间】:2012-05-25 11:19:40 【问题描述】:我得到hexTouchAreas
作为我的方法-drawHexagonTouchArea
的返回值,但是当我分析我的项目时,它显示CGMutablePathRef hexTouchArea = CGPathCreateMutable();
的行会产生警告:“在初始化期间存储到'hexTouchArea' 的值永远不会读”。在写有CGPathRelease(hexTouchArea);
的行中,它抱怨我不拥有那个对象,并且释放它是多余的。
另一个警告在 return path;
行,分析器说:“在第 629 行分配并存储到路径中的对象的潜在泄漏(即CGMutablePathRef path = CGPathCreateMutable();
)”
-(void)createTouchAreas
int realPositionsCount =[[GameStateSingleton sharedMySingleton]getSharedRealCountOfPositions];
hexTouchAreas = [[GameStateSingleton sharedMySingleton]getSharedHexTouchAreas];
NSMutableDictionary *existingHexagons = [[GameStateSingleton sharedMySingleton]getExistingHexagons];
for (int i = 0; i <= realPositionsCount;i++)
//create touchareas
NSString *hexKey = [NSString stringWithFormat:@"hexagon%d", i];
CCSprite *theSprite = [[existingHexagons objectForKey:hexKey]objectForKey:@"realSprite"];
CGPoint touchAreaOrigin = ccp(theSprite.position.x -22, theSprite.position.y-40);
NSString *touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i];
CGMutablePathRef hexTouchArea = CGPathCreateMutable();
hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin];
[hexTouchAreas setObject:(id)hexTouchArea forKey:touchAreaKey];
[[GameStateSingleton sharedMySingleton]setSharedHexTouchAreas:hexTouchAreas];
CGPathRelease(hexTouchArea);
创建和返回路径的方法:
-(CGMutablePathRef)drawHexagonTouchArea:(CGPoint)origin
CGMutablePathRef path = CGPathCreateMutable();
CGPoint newloc = CGPointMake(origin.x, origin.y);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x -22,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 0, newloc.y + 76);
CGPathAddLineToPoint(path, NULL, newloc.x + 46, newloc.y + 76);
CGPathAddLineToPoint(path, NULL, newloc.x +66,newloc.y + 40);
CGPathAddLineToPoint(path, NULL, newloc.x +44, newloc.y + 0);
CGPathCloseSubpath(path);
return path;
【问题讨论】:
【参考方案1】:使用以下代码,您可以创建一个可变路径,将其分配给 hexTouchArea,然后用另一个创建的对象覆盖 hexTouchArea。
CGMutablePathRef hexTouchArea = CGPathCreateMutable();
hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin];
您可能想要使用以下内容。
CGMutablePathRef hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];
【讨论】:
以上是关于释放 CGMutablePathRef 是有问题的的主要内容,如果未能解决你的问题,请参考以下文章
通过 CGMutablePathRef 为 drawRect 中绘制的框设置动画
在我的自定义 UIView 中,设置子视图的 backgroundColor 会导致它覆盖我的 drawRect 绘图(CGContextRef、CGMutablePathRef 等)。怎么修?