Graphics Programs Reference
In-Depth Information
Memory Management
If heap memory were infinite, we could create all the objects we needed and have them ex-
ist for the entire run of the application. But an application gets only so much heap memory,
and memory on an iOS device is especially limited. So it is important to destroy objects
that are no longer needed to free up and reuse heap memory. On the other hand, it is critical
not to destroy objects that are still needed.
The idea of object ownership helps us determine whether an object should be destroyed.
An object with no owners should be destroyed. An ownerless object cannot be sent
messages and is isolated and useless to the application. Keeping it around wastes
precious memory. This is called a memory leak .
An object with at least one owner must not be destroyed. If an object is destroyed
but another object or method still has a pointer to it (or, more accurately, a pointer
to where it used to live), then you have a very dangerous situation: sending a mes-
sage to an object that no longer exists will crash your application. This is called
premature deallocation .
Using ARC for memory management
The good news is that you don't need to keep track of who owns whom and what pointers
still exist. Instead, your application's memory management is handled for you by automatic
reference counting , or ARC.
In both projects you've built in Xcode so far, you've made sure to Use Automatic Referen-
ce Counting when creating the project ( Figure 3.4 ). This won't change; all of your projects
in this topic will use ARC for managing your application's memory.
Figure 3.4 Naming a new project
 
Search WWH ::




Custom Search