CAAnimation runs callbacks on released CALayer -- Mac OS X

176 views Asked by At

In an effort to avoid modal behavior, I allow the user to interrupt a running CABasicAnimation, and in the context of this application, the user's action calls for a new state in which existing CALayers should no longer exist. I am not using ARC (and maybe that is my problem, but bear with this old-school question).

When the user takes action, for a group of active CALayers I do this:

for(CALayer *c in layerArray){
    [c removeAllAnimations];
    [c removeFromSuperlayer];
    [c release];
}
[layerArray removeAllObjects]; 

(Trust me that, for better or for worse, this is not an over-release of the CALayers). Upon return from this routine and giving control back to the system, though, the system calls the internal OS routine run_animation_callbacks and crashes because it's trying to send messages to a zombie -- one of the CALayers that has been removed and released. Why isn't [c removeAllAnimations] sufficient to tell the OS to stop running callbacks on that object?

Thanks.

1

There are 1 answers

0
RedMarbleGames On BEST ANSWER

Figured it out, leaving the answer here in case it is helpful to others.

When a CAAnimation is removed (with the removeAllAnimations call in the code above), it calls the delegate's animationDidStop:finished: method to tell the delegate that the animation has been interrupted. (Interestingly, it does so even if you set the CALayer's delegate to nil when removing the animations).

So you can't removeAllAnimations and release the object at the same time. Instead, I suppose the animationDidStop:finished: method will have to have some method of detecting whether it should process as normal or not.