Chapter 31. Garbage Collection

As long as you use only Objective-C objects, the garbage collector will do exactly what you want, without your needing to think about it. However, as soon as you start to malloc C data types and Core Foundation structures, you will need to be a bit more circumspect.

When the garbage collector runs, it is looking for unreachable objects. You can think of the objects in your applications as a directed graph: This object knows that object; it knows those objects; and they know those other objects. So, the garbage collector starts with the pointers on the stack and the global variables and wanders this directed graph until it has recorded every “reachable” object. The unreachable objects are deallocated. See Figure 31.1 ...

Get Cocoa Programming for Mac OS X, Third 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.