سؤال

Question: If the ViewController containing the UIView to which the animation below was applied to was deallocated, would it cause a memory leak or retain cycle?

In other words, if I applied this animation to a uiview, would it cause a memory leak or retain cycle when the uiview's parent VC is dismissed or deallocated?

+(CAAnimation*)fadeOfRoomStatusLabel
{
    //Customize animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.FromValue = [NSNumber numberWithFloat:0.2f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.autoreverses = YES;
    //animation.BeginTime = CACurrentMediaTime()+.8;
    //animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut];
    animation.removedOnCompletion = NO;
    animation.duration = 1;
    animation.repeatCount = 99;
    return animation; 
}
هل كانت مفيدة؟

المحلول

No, it wouldn't since it doesn't have any explicitly set reference back to the view it's attached to. However, if you later set the animation's delegate to an object that has a strong reference to the animation (directly or indirectly), you will have a retain cycle, since the animation instance will retain its delegate. You will have to clear the delegate at some point in order for it to be released.

It's very easy to test these. Simply add a debug logging message to your view controller's -dealloc method. When you dismiss your view controller, ensure that you see the log message from its -dealloc method. If you don't, you know you have a memory issue with that view controller somewhere, and you can begin debugging why.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top