Ios 使用CGPathCreateCopyByTransforming和CGPathCreateCopy导致挤压内存泄漏
我正在使用圆弧和while循环来调整几个cgpathref,直到它们符合我所拥有的一些约束,并且一旦while循环的条件得到满足,我需要访问路径 有人能解释一下我的代码是怎么泄漏的吗 CGCreateCopy的文档。。。职能部门说他们会创建一个新的副本,由您负责发布。因此,我相信我的代码路径(while循环的结尾)确实释放了路径引用,而这些路径引用是仪器捕获并将泄漏的对象归因于这些路径引用的 下面的代码中表示了仪器的调用,在仪器突出显示的位置下方有注释 此例程给定一个GCSize大小(矩形大小)和一个int值(最终将在此处创建的形状中显示的数值)(形状的尺寸在某种程度上取决于此值的字符串表示的大小) 我应该注意,代码确实生成了我想要的所需路径——我没有要求任何人调试这个块!但它泄漏了,我不明白为什么Ios 使用CGPathCreateCopyByTransforming和CGPathCreateCopy导致挤压内存泄漏,ios,memory-leaks,Ios,Memory Leaks,我正在使用圆弧和while循环来调整几个cgpathref,直到它们符合我所拥有的一些约束,并且一旦while循环的条件得到满足,我需要访问路径 有人能解释一下我的代码是怎么泄漏的吗 CGCreateCopy的文档。。。职能部门说他们会创建一个新的副本,由您负责发布。因此,我相信我的代码路径(while循环的结尾)确实释放了路径引用,而这些路径引用是仪器捕获并将泄漏的对象归因于这些路径引用的 下面的代码中表示了仪器的调用,在仪器突出显示的位置下方有注释 此例程给定一个GCSize大小(矩形大小)
float lineThickness = size.width;
float perimeterStrokeWidth = lineThickness * 2;
CGSize shadowOffset = CGSizeMake( perimeterStrokeWidth / 1.5, perimeterStrokeWidth * 1.25 );
float shadowBlur = perimeterStrokeWidth * 2.0;
NSString *edgeSymbol = @"°";
CGPathRef rightEdgePath = NULL;
CGPathRef leftEdgePath = NULL;
CGPathRef valueStringPath = NULL;
CGAffineTransform adjust, flip;
float pointsize, deltaX, deltaY, inset, pointSizeThatFits;
CGRect valueRect, edgeRect, leftEdgeRect, rectForValue;
CGSize trialSize = size;
pointsize = size.height;
CGPoint tdc; // top dead center
CGPoint bdc; // bottom dead center
NSLog(@"\n\n");
BOOL done = false;
int attempts = 0;
int attemptsRemaining = 11; // ensure no infinite looping
while ( ! done ) {
done = YES; // presumptive close! tests below will reset if they fail
--attemptsRemaining;
trialSize = CGSizeMake(trialSize.width, trialSize.height - attempts);
++attempts;
lineThickness = trialSize.height / 60;
perimeterStrokeWidth = lineThickness * 2;
shadowOffset = CGSizeMake( perimeterStrokeWidth / 1.5, perimeterStrokeWidth * 1.0 );
shadowBlur = perimeterStrokeWidth * 2.0;
pointsize = [UIFont sizeFont:[UIFont fontWithName:@"Alameda"
size:pointsize]
toFitText:edgeSymbol
withinRect:CGRectMake(0,
0,
trialSize.width,
trialSize.height)];
// sizeFont:toFitText:withinRect: is a custom addition to UIFont that
// recursively tries smaller and smaller pointsize values until the
// text fits within the rectangle provided.
CGPathRef tmpRightEdgePath = [edgeSymbol newPathWithFont:[UIFont fontWithName:@"Alameda" size:trialSize.height * 1.1]];
// newPathWithFont: is a custom addition to NSString that returns a
// CGPathRef representing the glyphs that make up the string, rendered in
// the font (and size) specified
flip = CGAffineTransformMakeScale(-1.0, 1.0);
CGPathRef tmpLeftEdgePath = CGPathCreateCopyByTransformingPath(tmpRightEdgePath, &flip);
edgeRect = CGPathGetBoundingBox(tmpRightEdgePath);
// Center the symbol vertically in the size we were given (maybe up a little because of the shadow below?)
deltaY = ( (size.height / 2) - (edgeRect.size.height / 2) - edgeRect.origin.y);
// Slide it over to the right edge, inset slightly for the shadow
inset = shadowOffset.width + shadowBlur;
deltaX = ( trialSize.width - edgeRect.origin.x - edgeRect.size.width - inset);
adjust = CGAffineTransformMakeTranslation(deltaX, deltaY);
CGPathRef tmpRightEdgePath1 = CGPathCreateCopyByTransformingPath(tmpRightEdgePath, &adjust);
CGPathRelease(tmpRightEdgePath);
tmpRightEdgePath = NULL;
edgeRect = CGPathGetBoundingBox(tmpRightEdgePath1);
leftEdgeRect = CGPathGetBoundingBox(tmpLeftEdgePath);
deltaX = inset - leftEdgeRect.origin.x;
adjust = CGAffineTransformMakeTranslation(deltaX, deltaY);
CGPathRef tmpLeftEdgePath1 = CGPathCreateCopyByTransformingPath(tmpLeftEdgePath, &adjust);
CGPathRelease(tmpLeftEdgePath);
tmpLeftEdgePath = NULL;
leftEdgeRect = CGPathGetBoundingBox(tmpLeftEdgePath1);
rectForValue = CGRectMake(leftEdgeRect.origin.x + leftEdgeRect.size.width,
leftEdgeRect.origin.y,
edgeRect.origin.x - leftEdgeRect.origin.x - leftEdgeRect.size.width,
leftEdgeRect.size.height);
NSString *valueString = [NSString stringWithFormat:@"%i", value];
pointSizeThatFits = [UIFont sizeFont:[UIFont fontWithName:@"Futura" size:trialSize.height]
toFitText:valueString
withinRect:rectForValue];
valueStringPath = [valueString newPathWithFont:[UIFont fontWithName:@"Futura" size:pointSizeThatFits]];
valueRect = CGPathGetBoundingBox(valueStringPath);
deltaY = ( (size.height / 2) - (valueRect.size.height / 2) - valueRect.origin.y);
deltaX = ( (trialSize.width / 2) - (valueRect.size.width / 2) - valueRect.origin.x);
adjust = CGAffineTransformMakeTranslation(deltaX, deltaY);
CGPathRef tmpValueStringPath1 = CGPathCreateCopyByTransformingPath(valueStringPath, &adjust);
CGPathRelease(valueStringPath);
valueStringPath = NULL;
valueRect = CGPathGetBoundingBox(tmpValueStringPath1);
CGPathRef tmpRightEdgePath2;
CGPathRef tmpLeftEdgePath2;
if ((valueRect.origin.x + valueRect.size.width) < edgeRect.origin.x) {
float gapToClose = edgeRect.origin.x - (valueRect.origin.x + valueRect.size.width);
adjust = CGAffineTransformMakeTranslation(- gapToClose, 0);
{
tmpRightEdgePath2 = CGPathCreateCopyByTransformingPath(tmpRightEdgePath1, &adjust);
// ^^^^ Instruments reports 5.9% of leaks here
CGPathRelease(tmpRightEdgePath1);
tmpRightEdgePath1 = NULL;
}
adjust = CGAffineTransformMakeTranslation(gapToClose, 0);
{
tmpLeftEdgePath2 = CGPathCreateCopyByTransformingPath(tmpLeftEdgePath1, &adjust);
// ^^^^ Instruments reports 23.5% of leaks here
CGPathRelease(tmpLeftEdgePath1);
tmpLeftEdgePath1 = NULL;
}
leftEdgeRect = CGPathGetBoundingBox(tmpLeftEdgePath2);
} else {
{
tmpRightEdgePath2 = CGPathCreateCopy(tmpRightEdgePath1);
CGPathRelease(tmpRightEdgePath1);
tmpRightEdgePath1 = NULL;
}
{
tmpLeftEdgePath2 = CGPathCreateCopy(tmpLeftEdgePath1);
CGPathRelease(tmpLeftEdgePath1);
tmpLeftEdgePath1 = NULL;
}
leftEdgeRect = CGPathGetBoundingBox(tmpLeftEdgePath2);
}
tdc.x = CGRectGetMidX(rectForValue);
bdc.x = tdc.x;
tdc.y = leftEdgeRect.origin.y - (tdc.x - leftEdgeRect.origin.x) * 0.08; // SWAG on the 10% of width
if ((tdc.y - lineThickness/2.0) < 0) {
done = NO;
}
bdc.y = leftEdgeRect.origin.y + leftEdgeRect.size.height + (leftEdgeRect.origin.y - tdc.y);
float shadowness = shadowOffset.height;
if ((bdc.y + shadowness) > size.height) {
done = NO;
}
if (attemptsRemaining <= 0) {
done = YES;
}
// And finally, assign them out if DONE!
if (done) {
leftEdgePath = CGPathCreateCopy(tmpLeftEdgePath2);
// ^^^^ Instruments reports 35.3% of leaks here
rightEdgePath = CGPathCreateCopy(tmpRightEdgePath2);
// ^^^^ Instruments reports 35.3% of leaks here
valueStringPath = CGPathCreateCopy(tmpValueStringPath1);
}
CGPathRelease(tmpLeftEdgePath2);
tmpLeftEdgePath2 = NULL;
CGPathRelease(tmpRightEdgePath2);
tmpRightEdgePath2 = NULL;
CGPathRelease(tmpValueStringPath1);
tmpValueStringPath1 = NULL;
}
// what follows is the application of those paths within CGContext drawing calls.
float lineThickness=size.width;
浮动周长行程宽度=线宽*2;
CGSize shadowOffset=CGSizeMake(周长行程宽度/1.5,周长行程宽度*1.25);
浮动阴影模糊=周长行程宽度*2.0;
NSString*edgeSymbol=@“°”;
CGPathRef rightEdgePath=NULL;
CGPathRef leftEdgePath=NULL;
CGPathRef valueStringPath=NULL;
CGA仿射变换调整、翻转;
浮动点大小、deltaX、deltaY、插图、pointSizeThatFits;
CGRect valueRect,Edg直立,LeftEdg直立,rectForValue;
CGSize trialSize=尺寸;
pointsize=size.height;
CGPoint tdc;//上止点
CGPoint bdc;//下止点
NSLog(@“\n\n”);
BOOL done=false;
int=0;
int attemptsRemaining=11;//确保没有无限循环
而(!完成){
done=YES;//假定关闭!如果以下测试失败,将重置它们
--试图保持;
trialSize=CGSizeMake(trialSize.width,trialSize.height-尝试次数);
++企图;
线宽=trialSize.height/60;
周长行程宽度=线宽*2;
阴影偏移=CGSizeMake(周长行程宽度/1.5,周长行程宽度*1.0);
阴影模糊=周长行程宽度*2.0;
pointsize=[UIFont sizeFont:[UIFont fontWithName:@“Alameda”
大小:点大小]
toFitText:edgeSymbol
withinRect:CGRectMake(0,
0,
trialSize.width,
三化高度);
//sizeFont:toFitText:withinRect:是对UIFont的自定义添加,用于
//递归地尝试越来越小的pointsize值,直到
//文本适合所提供的矩形。
CGPathRef tmpRightEdgePath=[edgeSymbol newPathWithFont:[UIFont fontWithName:@“Alameda”大小:trialSize.height*1.1]];
//newPathWithFont:是对NSString的自定义添加,返回
//CGPathRef表示组成字符串的图示符,以
//指定的字体(和大小)
翻转=CGAffineTransformMakeScale(-1.0,1.0);
CGPathRef tmpLeftEdgePath=CGPathCreateCopyByTransformingPath(tmprightdgePath,&flip);
Edg直立=CGPathGetBoundingBox(TMPrightEdge路径);
//以给定的尺寸垂直居中符号(可能因为下面的阴影而稍微向上?)
deltaY=((size.height/2)-(edgristle.size.height/2)–edgristle.origin.y);
//将其滑动到右侧边缘,稍微插入阴影
插图=阴影偏移。宽度+阴影模糊;
deltaX=(trialSize.width-edgirst.origin.x-edgirst.size.width-inset);
调整=CGAffineTransformMakeTransform(deltaX,deltaY);
CGPathRef tmpRightEdgePath1=CGPathCreateCopyByTransformingPath(tmpRightEdgePath,&adjust);
CGPathRelease(tmpRightEdgePath);
tmpRightEdgePath=NULL;
Edg直立=CGPathGetBoundingBox(TMPrightEdge路径1);
leftedgrist=CGPathGetBoundingBox(tmpLeftEdgePath);
deltaX=插图-leftedgristing.origin.x;
调整=CGAffineTransformMakeTransform(deltaX,deltaY);
CGPathRef tmpLeftEdgePath1=CGPathCreateCopyByTransformingPath(tmpLeftEdgePath,&adjust);
CGPathRelease(tmpLeftEdgePath);
tmpLeftEdgePath=NULL;
LeftedGrindle=CGPathGetBoundingBox(tmpLeftEdgePath1);
rectForValue=CGRectMake(LeftedGright.origin.x+LeftedGright.size.width,
leftedging.origin.y,
Edgfright.origin.x-LeftEdgfright.origin.x-LeftEdgfright.size.width,
左侧(直立、大小、高度);
NSString*valueString=[NSString stringWithFormat:@“%i”,value];
pointSizeThatFits=[UIFont sizeFont:[UIFont fontWithName:@“Futura”大小:trialSize.height]
toFitText:valueString
withinRect:rectForValue];
valueStringPath=[valueString newPathWithFont:[UIFont fontWithName:@“Futura”大小:pointSizeThatFits]];
valueRect=CGPathGetBoundingBox(valueStringPath);
deltaY=((size.height/2)-(valueRect.size.height/2)-(valueRect.origin.y);
deltaX=((trialSize.width/2)-(valueRect.size.width/2)–valueRect.origin.x);
调整=CGAffineTransformMakeTransform(deltaX,deltaY);
CGPathRef tmpValueStringPath1=CGPathCreateCopyByTransformingPath(valueStringPath,&adjust);
CGPathRelease(valueStringPath);
valueStringPath=NULL;
valueRect=CGPathGetBoundingBox(tmpValueStringPath1);
CGPathRef tmpRightEdgePath2;
CGPathRef tmpLeftEdgePath2;
if((valueRect.origin.x+valueRect.size.width)valueStringPath = CGPathCreateCopy(tmpValueStringPath1);
leftEdgePath = CGPathCreateCopy(tmpLeftEdgePath2);
rightEdgePath = CGPathCreateCopy(tmpRightEdgePath2);
CGPathRef tmpRightEdgePath2;
CGPathRef tmpLeftEdgePath2;
CGPathRef tmpRightEdgePath2 = NULL;
CGPathRef tmpLeftEdgePath2 = NULL;
# Category Event Type RefCt ResponsibleLibrary Responsible Caller
0 NSMutableRLEArray Malloc 1 Foundation -[NSConcreteAttributedString initWithString:attributes:]
1 NSMutableRLEArray Release 0 Foundation -[NSConcreteAttributedString dealloc]
2 NSMutableRLEArray Free 0 Foundation -[NSRLEArray dealloc]
3 CGPath Malloc 1 CoreGraphics CGTypeCreateInstance
4 CGPath CFRelease 0 my app my routine
5 CGPath Free 0 my app my routine
6 CGPath Malloc 1 CoreGraphics CGTypeCreateInstance
7 CGPath CFRelease 0 my app my routine
8 CGPath Free 0 my app my routine
9 CGPath Malloc 1 CoreGraphics CGTypeCreateInstance
CGAffineTransform slightUp = CGAffineTransformMakeTranslation(0, - lineThickness * 2 );
leftEdgePath = CGPathCreateMutableCopyByTransformingPath(leftEdgePath, &slightUp);
rightEdgePath = CGPathCreateMutableCopyByTransformingPath(rightEdgePath, &slightUp);
CGAffineTransform slightUp = CGAffineTransformMakeTranslation(0, - lineThickness * 2 );
CGMutablePathRef finalLeftEdgePath = CGPathCreateMutableCopyByTransformingPath(leftEdgePath, &slightUp);
CGPathRelease(leftEdgePath);
CGMutablePathRef finalRightEdgePath = CGPathCreateMutableCopyByTransformingPath(rightEdgePath, &slightUp);
CGPathRelease(rightEdgePath);