1

任何人都可以帮助我理解为什么我在运行分析时遇到这个问题吗?

我有一个存储在“oldShadowPath”中的对象的潜在泄漏。

-(void) layoutShadowWithDuration:(NSTimeInterval)duration
{
    CGPathRef oldShadowPath = self.mainViewController.view.layer.shadowPath;

    if (oldShadowPath)
    {
        CFRetain(oldShadowPath);
    }

    // Update shadow path for the view
    CGPathRef path = [UIBezierPath bezierPathWithRect:self.mainViewController.view.bounds].CGPath;
    self.mainViewController.view.layer.shadowPath = path;

    // You would think setting duration to 0 would cause the animation added below to not animate. You would be wrong.
    if (duration != 0) {
        if (oldShadowPath)
        {
            [self.mainViewController.view.layer addAnimation:((^ {
                CABasicAnimation *transition = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
                transition.fromValue = (__bridge id)oldShadowPath;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.duration = duration;
                return transition;
            })()) forKey:@"transition"];

            CFRelease(oldShadowPath);
        }
    }
}
4

2 回答 2

1

这是因为您没有与每个 CFRetain 对应的 CFRelease。您在双重“if”语句中释放对象,其中一个独立于保留条件(“oldShadowPath”)。

于 2013-11-02T01:46:40.457 回答
-1

正如@H2CO3 评论的那样,如果duration为 0,则oldShadowPath永远不会被释放。

于 2013-11-02T01:47:50.110 回答