简体   繁体   English

该动画会导致泄漏或保持周期吗?

[英]Will This CAAnimation Cause a Leak or Retain Cycle?

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? 问题:如果释放了包含以下动画要应用到的UIView的ViewController,会导致内存泄漏或保留周期吗?

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? 换句话说,如果我将此动画应用于uiview,当uiview的父VC被解雇或释放时,是否会导致内存泄漏或保留周期?

+(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. 只需将调试日志消息添加到视图控制器的-dealloc方法即可。 When you dismiss your view controller, ensure that you see the log message from its -dealloc method. 关闭视图控制器时,请确保从其-dealloc方法中看到日志消息。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM