For the More Curious: Memory Management and Blocks

There are some subtle rules when it comes to managing memory for blocks. To understand how the block manages memory, you must understand the difference between the heap and the stack portions of memory.

To allocate memory from the heap, you must explicitly do so. Typically, this is done by sending the message alloc to a class, but it is also done when you call malloc or one of its friends. Sometimes, this happens in the implementation of another function or method, like UIGraphicsBeginImageContextWithOptions (because it calls malloc). When you allocate memory from the heap, it is your responsibility to clean it up. This is done by deallocating an object, freeing a malloc’ed buffer, or calling ...

Get iOS Programming: The Big Nerd Ranch Guide, Second Edition 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.