• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用 CGPathCreateCopyByTransforming 和 CGPathCreateCopy 导致的挤压内存泄漏

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:52:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 ARC 和 while 循环来调整几个 CGPathRef,直到它们符合我的某些约束条件,并且一旦满足 while 循环的条件,我就需要访问路径。

谁能解释我的代码是如何泄漏的?

CGCreateCopy... 函数的文档说它们会创建一个新副本,您负责发布该副本。所以我相信我的代码路径(while 循环的结尾)确实释放了 Instruments 正在捕获并将泄漏对象归因于的路径引用。

Instrument 调用在下面的代码中表示,并在 Instruments 突出显示的位置下方带有注释。

这个例程被赋予一个 GCSize 大小,它是一个矩形的大小和一个 int 值,它是一个最终将在此处创建的形状中显示的数值(形状的尺寸在某种程度上取决于字符串的大小这个值的表示)

我应该注意,代码确实会生成我想要的所需路径——我不是要求任何人调试这个 block !但它泄漏了,我不明白为什么。

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.

我从关于仪器和泄漏的其他有用答案中了解到,标记的行通常是创建即将泄漏的对象的位置。考虑到这一点,我向下看我可能会在没有首先释放先前路径的情况下分配新路径,但没有任何(我可以看到 - 但我承认我有点睡眼惺忪看着这个)。



Best Answer-推荐答案


为了其他发现自己被内存泄漏问题困扰的人的利益,我将在此处提供我的问题的答案,因为侦查过程可能会对您有所帮助。

在代码 list 的中间,您可以看到我在哪里通知编译器我将很快使用两个 CGPathRef,最初在 if/then/else 的范围内,所以我需要在该 block 之外分配(?)所以它们在我的日常生活中是本地的。

CGPathRef tmpRightEdgePath2;
CGPathRef tmpLeftEdgePath2;

通过将它们更改为:

CGPathRef tmpRightEdgePath2 = NULL;
CGPathRef tmpLeftEdgePath2 = NULL;

这清除了在 if/then/else 语句期间报告的两个泄漏。

所以现在我剩下的两个泄漏报告在 if (done) {} block 内。

在 Instruments 中,查看 Leaks 工具,Cycles & Roots -> Leak Cycles -> foo History 我看到 10 个内存事件以 RefCt 为 1 结束,因此很明显是泄漏:

#  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

所以在我认为我已经完成了路径操作之后的某个时刻,必须发生另一个 CGCreateCopy... 样式调用,因为在我发布所有内容之后, 调用了 Malloc。因此,通过使用搜索/查找,我通过绘图代码继续前进,并找到了最后一个 CGPathCreateCopyByTransforming:

CGAffineTransform slightUp = CGAffineTransformMakeTranslation(0, - lineThickness * 2 );
leftEdgePath = CGPathCreateMutableCopyByTransformingPath(leftEdgePath, &slightUp);
rightEdgePath = CGPathCreateMutableCopyByTransformingPath(rightEdgePath, &slightUp);

由于保留计数在该复制期间增加了 1,因此可以保证泄漏。所以我把它们改成了:

CGAffineTransform slightUp = CGAffineTransformMakeTranslation(0, - lineThickness * 2 );
CGMutablePathRef finalLeftEdgePath = CGPathCreateMutableCopyByTransformingPath(leftEdgePath, &slightUp);
CGPathRelease(leftEdgePath);
CGMutablePathRef finalRightEdgePath = CGPathCreateMutableCopyByTransformingPath(rightEdgePath, &slightUp);
CGPathRelease(rightEdgePath);

然后我更改了我的 CGContextAddPath() 以匹配新的“最终”路径,以及最终发布调用以发布“最终”版本并且我没有泄漏!

关于ios - 使用 CGPathCreateCopyByTransforming 和 CGPathCreateCopy 导致的挤压内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601239/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap