CAAnimation delegate

Discussion in 'iOS Programming' started by xcodeNewbie, Apr 17, 2012.

  1. xcodeNewbie macrumors member

    Joined:
    Jul 1, 2011
    #1
    I'm having some problems concerning memory management with a CAAnimation delegate. I add an animation to an image view on screem with the following code:
    Code:
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
            anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
            anim.fromValue = [NSValue valueWithCGPoint:aShip.center];
            anim.toValue = [NSValue valueWithCGPoint:aPlanet.center];
            anim.duration = time;
            anim.delegate = aShip;
            anim.removedOnCompletion = YES;
            [aShip.layer addAnimation:anim forKey:@"fly"]; 
    The animation retains its delegate, the ship. In animationDidStop:Finished: I try to set the delegate to nil so my ship will be properly released, but it says that the animation is readonly and I cannot change the delegate. How can I make it so the animation will release its delegate?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    I would expect that since you set the removedOnCompletion to YES, that as part of the cleanup, the animation would release the delegate. My reasoning is simple enough; anim is created as a non-retained object, it gets retained when adding it to the layer, and when the layer removes it, it would release it which would cause anim to perform it's own cleanup which would include releasing the delegate.

    What evidence do you have that the delegate is causing your memory problems?
     
  3. xcodeNewbie thread starter macrumors member

    Joined:
    Jul 1, 2011
    #3
    Retain Count

    In the animationDidStop:Finished: method the ships's retain count is 1, and it is still being retained by the animation. The ship is never dealloced, I used instruments to check.
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    Oh, you want to remove aShip. Here is what I'd try.

    At the end of the animationDidStop:Finished: method try adding
    Code:
    [self removeFromSuperview];
    Since the delegate is aShip that is the object with the animationDidStop:Finished: method that gets called, and self at that point will be the proper object to act on.
     

Share This Page