Block is divided into three kinds of nsglobalblock,nsstackblock, Nsmallocblock.
- Nsglobalblock: Similar function, in text segment;
- Nsstackblock: In stack memory, block will be invalid after function return;
- Nsmallocblock: In heap memory.
Nsglobalblock we can identify by referencing an external variable, which is nsglobalblock if the external variable is not referenced, and can be used as a function. Otherwise it is nsstackblock.
Nsmallocblock only need to copy the Nsstackblock to get it, but retain operation is not possible
- Block_copy is equivalent to copy, and Block_release is equivalent to release.
- The reference count is not changed for block retain, copy, Release Retaincount,retaincount is always 1;
- Nsglobalblock:retain, copy, release operations are not valid;
- Nsstackblock:retain, release operation is not valid, it must be noted that nsstackblock after the function is returned, block memory will be recycled. Even retain is useless. The easy mistake is [[Mutableaarry Addobject:stackblock], (complement: Do not worry about this in arc, because the instantiated block is copied to the heap by default in arc) after the function is out of the stack, The stackblock that were taken from the Mutableaarry have been recycled and turned into wild pointers. The correct approach is to copy the Stackblock to the heap first, then add the array: [Mutableaarry addobject:[[stackblock copy] autorelease]. A new Nsmallocblock type object is generated after Copy,copy is supported.
- Nsmallocblock supports retain, release, although Retaincount is always 1, the memory manager will still increase and decrease the count. No new objects are generated after copy, only one reference is added, similar to retain;
- Try not to use the retain operation on the block.
For more detailed block content, please visit the original blog: http://www.cnblogs.com/hanjun/p/3767394.html
IOS block types and switching