Use: @ property (nonatomic, copy)
I made a mistake on this issue today. I have been searching for a long time to find out the cause.
In addition, after a simple disassembly, the Block is stored in the static variable area, and a runtime stack is constructed during runtime for calling.
Retain does not change the reference count of the Block. Therefore, applying retain to the Block is equivalent to assign.
But why does EXC_BAD_ACCESS appear in the static storage area? The code is all there.
It is said on the Internet that the Block is on the stack, which should be wrong: the pointer pointing to the Block code is on the stack.
I think the reason is as follows:
Code execution in the static zone requires special structures, such as loading to registers and adjusting ESP.
The Code on the stack can be directly executed.
We look forward to more detailed explanations.
When storing blocks in properties, arrays or other data structures, there's an important difference between usingcopy
Orretain
. And in short, you shoshould always usecopy
.
When blocks are first created, they are allocated on the stack. If the block is called when that stack frame has disappeared, it can have disastrous consequences, usually a EXC_BAD_ACCESS or something plain weird.
If youretain
A stack allocated block (as they all start out being), nothing happens. It continues to be stack allocated and will crash your app when called. However, if youcopy
A stack allocated block, it will copy it to the heap, retaining references to local and instance variables used in the block, and calling it will behave as expected. However, if youcopy
A heap allocated block, it doesn' t copy it again, it justretains
It.
So you shoshould always declare your blocks as properties like this:
@property (copy, ...) (int)(^aBlock)();
And never like this:
@property (retain, ...) (int)(^aBlock)();
And when providing blocksNSMutableArray
S and the like, alwayscopy
, Neverretain
.