核心图像内存泄漏

3
分析器提示出现内存问题。通常我会使用自动释放,但在核心基础框架中无法这样做。我该如何解决这个错误?
- (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;
}

在按照建议添加发布路径代码后,我遇到了另一个错误和原始错误?additional screen shot
5个回答

4
CFRelease(path);

CoreFoundation参考

在不再需要路径时,请使用CFRelease。

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

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

  • alloc
  • copy
  • new
  • retain

因此,您应该将方法命名为: newCGMutablePathRefCreateWithRoundedRectForRect 而不是: NewCGMutablePathRefCreateWithRoundedRectForRect


谢谢回复。如果在返回之前释放它,它会返回NULL,那么我应该在什么时候释放它呢? - geminiCoder
不,你必须在其他时间发布它,参见更新的答案。 - peko
我尝试了,但遇到了另一个错误,请参见上面的编辑。 - geminiCoder
这是因为你的方法命名,再看更新后的答案。 - peko

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

1

按照以下两个简单步骤解决此问题:

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

0
CGPathCloseSubpath(path);

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

哦,那种情况下你可以使用 CGPathRelease - Baby Groot
那不会返回 NULL 吗? - geminiCoder

0

在使用完路径变量后,请释放它。例如:

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

比如说:

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

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

CFRelease(path);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接