Graphics Programs Reference
In-Depth Information
Recall that instance variables live in the heap as part of an object. When an object gets
destroyed, its instance variables are also destroyed, and any object that was pointed to by
one of those instance variables loses an owner.
Local variables live in the method's frame. When a method finishes executing and its
frame is popped off the stack, any object that was pointed to by one of these local vari-
ables loses an owner.
There is one more important way an object can lose an owner. Recall that an object in a
collection object, like an array, is owned by the collection object. When you remove an
object from a mutable collection object, like an NSMutableArray , the removed object
loses an owner.
[items removeObject:p];
// object pointed to by p loses an owner
Keep in mind that losing an owner doesn't necessarily mean that the object gets des-
troyed; if there is still another pointer to the object somewhere, then that object will con-
tinue to exist. However, when an object loses its last owner, it means certain and appropri-
ate death.
Because objects own other objects, which can own other objects, the destruction of a
single object can set off a chain reaction of loss of ownership, object destruction, and free-
ing up of memory.
We have an example of this in RandomPossessions . Take another look at the object dia-
gram of this application.
Figure 3.6 Objects and pointers in RandomPossessions
Search WWH ::




Custom Search