【问题标题】:Core Graphics memory leak核心显卡内存泄漏
【发布时间】:2013-07-29 00:28:03
【问题描述】:

分析器正在标记内存问题。通常我会使用自动释放,但这在 Core Foundation 中是不可能的。如何解决此错误?

- (CGMutablePathRef)NewCGMutablePathRefCreateWithRoundedRectForRect:(CGRect)rect andRadius:(CGFloat)radius andMargin:(CGFloat)margin andrIndent:(CGFloat)rIndent andlIndent:(CGFloat)lIndent
{
CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect) + margin);
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMinY(rect) + margin, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMaxY(rect) - margin, radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect) - margin - rIndent, CGRectGetMaxY(rect) - margin, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMaxY(rect) - margin, radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMaxY(rect) - margin, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMinY(rect) + margin, radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect) + margin + lIndent, CGRectGetMinY(rect) + margin, CGRectGetMaxX(rect) - margin, CGRectGetMinY(rect) +margin, radius);
CGPathCloseSubpath(path);


return path;
}

按照建议添加发布路径代码后,我得到另一个错误加上原来的错误?

【问题讨论】:

    标签: ios memory-leaks core-graphics


    【解决方案1】:
    CFRelease(path);
    

    CoreFoundation reference

    在不再需要路径后使用 CFRelease。

    CGMutablePathRef path = [obj NewCGMutablePathRefCreateWithRoundedRectForRect:rect andRadius:radius andMargin:margin andrIndent:rIndent andlIndent:lIndent];
    //do something with path, then release it
    CFRelease(path);
    

    另一件事是返回非自动释放对象的方法必须以:

    • 分配
    • 复制
    • 新的
    • 保留

    所以你应该命名你的方法: newCGmutablePathRefCreateWithRoundedRectForRect 代替: NewCGMutablePathRefCreateWithRoundedRectForRect

    【讨论】:

    • 感谢您的回复。我应该在什么时候释放它,如果我在返回之前释放它,它会返回NULL?
    • 不,您必须在其他时间发布它,请参阅更新的答案
    • 我尝试过,但遇到了一个额外的错误,请参阅上面的编辑
    • 那是因为你的方法命名,再次查看更新的答案
    【解决方案2】:

    在外部创建路径并尝试在您的方法中仅传递此路径的引用。然后,您可以在调用方法的同一范围内释放路径。内存泄漏问题将消失。

    【讨论】:

      【解决方案3】:

      通过两个简单的步骤解决此问题:

      1. 将方法从NewCGMutablePathRefCreateWithRoundedRectForRect 重命名为CreateCGMutablePathRefWithRoundedRectForRect。重命名它的重要部分是该方法以Create 开头,它告诉静态分析器您的方法返回一个需要释放的对象并将停止您看到的警告。
      2. 通过调用CGPathRelease释放返回的CGPathRef

      【讨论】:

        【解决方案4】:
        CGPathCloseSubpath(path);
        

        在返回变量path之前添加此代码。

        【讨论】:

        • 哦。在这种情况下,您可以使用CGPathRelease
        • 不会返回 NULL 吗?
        【解决方案5】:

        使用完后释放路径变量。例如:

        在您的代码中的其他方法中,您将使用此路径 rt?

        CGMutablePathRef 路径 = [obj NewCGMutablePathRefCreateWithRoundedRectForRect:rect andRadius:radius andMargin:margin andrIndent:rIndent andlIndent:lIndent];

        ....... .......

        CFRelease(路径);

        【讨论】:

          猜你喜欢
          • 2011-09-19
          • 1970-01-01
          • 1970-01-01
          • 2015-11-05
          • 2011-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多