物体可能泄漏

时间:2013-11-02 01:06:15

标签: ios objective-c memory-leaks

有什么可以帮助我理解为什么我在运行analyze时遇到这个问题?

我可能会泄漏存储在“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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

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