Memory, Threads, and Blocks

When a block literal is assigned, memory for the block is automatically allocated on the stack. This memory takes a snapshot of the values of any variables from the enclosing scope used in the block, and captures a pointer to any variables that use the __block modifier. While the block exists on the stack, calling retain on that block will have no practical effect. To retain a block in memory either as a property on a class or as a parameter to a method call, the block must be copied, which will move it to the heap. After it’s on the heap, normal memory management rules apply.

Because blocks are initially allocated on the stack, there is a practical limit on the number of blocks that can be allocated at once. If blocks ...

Get iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.