关于iphone:处理CGMutablePath时出现内存泄漏

Memory Leak When Handling CGMutablePath

在以下情况下使用CGMutablePath时发生内存泄漏:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (CGMutablePathRef) textMutablePathForFrame:(CGRect)frame
{
    CGAffineTransform transform = CGAffineTransformMakeScale(frame.size.width / self.shapeMutablePathSize.width, frame.size.height / self.shapeMutablePathSize.height);

    return CGPathCreateMutableCopyByTransformingPath(self.shapeMutablePath, &transform);
}

- (CTFrameRef) textFrameForFrame:(CGRect)frame framesetter:(CTFramesetterRef)framesetter
{
    CGMutablePathRef textMutablePath = [self textMutablePathForFrame:frame];
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), textMutablePath, NULL);
    CGPathRelease(textMutablePath);

    return textFrame;
}

通过仪器分析,我在textMutablePathForFrame中带有" return "的行上发生了内存泄漏,该行显示"分配给第132行的对象的潜在泄漏"(第132行是返回行本身) 。

textFrameForFrame行" CGPathRelease(textMutablePath); "处也出现内存泄漏,它说:"调用者此时不拥有的对象的引用计数错误地减少了"。

无法解决这个问题,感觉就像我终于对Core中的内存管理有了很好的了解。

更新:看起来这可能是一个错误,将再次碰碰它一次,看看是否还有其他人有不同的感觉。


@JonathanCichon原则上是正确的,但是命名约定错误。 ObjC方法的正确命名约定是newMutablePathForFrame。分析仪是正确的。"创建"规则仅适用于Core Foundation。 ObjC命名约定在《高级内存管理编程指南》中。 Core Foundation规则稍有不同,其中包括"创建"规则,位于Core Foundation的内存管理编程指南中。


我认为您没有内存泄漏,请将您的textMutablePathForFrame方法名称更改为createMutablePathForFrame,并且警告会消失。