[myArray addObject: [[objcBlock copy] autorelease]] failed while freeing the array

I wrote a class to declaratively describe the sequence of animations of a UIView. My method takes vararg animation blocks and puts them in an array. So in my loop I want to do this:

[animations addObject:[[block copy] autorelease]];

First copyblock so that it moves to the heap, allowing it to be an retained array. Then I will automatically free it to give up ownership (because the array saves it).

However, this fails when the array of animations is dealloc'd. (I understand that the reference blocks have already been deleted.)

Strange, this works:

[animations addObject:[block copy]];
[block release];

UPDATE: - ... how it does:

[animations addObject:[block copy]];
[block autorelease];

Why? I would expect all 3 pieces of code to work equally well. Any explanation?

+4
1

1:

[animations addObject:[[block copy] autorelease]];

.

2:

[animations addObject:[block copy]];
[block release];

, . , , ( ), .

3:

[animations addObject:[block copy]];
[block autorelease];

, . . .

, , - . .

+6

All Articles