Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
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?
 
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?
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.