I wrote a class for declaratively describing a sequence of UIView animations. My method takes a vararg of animation blocks and puts them in an array. So in my loop I want to do this:
[animations addObject:[[block copy] autorelease]];
I first copy
the block so that it is moved to the heap, allowing it to be retain
'ed by the array. Then I autorelease it to relinquish ownership (because the array retains it).
However this crashes when the animations array is dealloc'd. (My understanding is that the referenced blocks have already been dealloc'd.)
Strange thing is, this works:
[animations addObject:[block copy]];
[block release];
UPDATE: – … as does this:
[animations addObject:[block copy]];
[block autorelease];
Why? I would have expected all 3 code snippets to work equally well. Any explanation?