Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [26]

By Root 891 0
on the rules surrounding retain counts in order to work effectively in the ARC environment. If you want to write code for versions of Mac OS X prior to 10.6 or iOS prior to 4.0, you’ll need to know how to use retain counts.

Living with Manual Reference Counting


Retain counts are a pretty simple concept. Every object in Objective-C has a retain count. The retain count is an integer. When an object is created by the alloc method, the retain count is set to 1. When the retain count becomes zero, the object is deallocated. You increment the retain count by sending the message retain to the object. You decrement the retain count by sending the message release to the object.

An object’s retain count should represent how many other objects have references to it. When the retain count becomes zero, this indicates that no one cares about it any more. It is deallocated so that the memory it was occupying can be reused.

A commonly used analogy is that of the dog and the leash. Each person who wants to ensure that the dog will stay around retains the dog by attaching a leash to its collar. Many people can retain the dog; as long as at least one person is retaining the dog, the dog will not go free. When zero people are retaining the dog, it will be deallocated. The retain count of an object, then, is the number of “leashes” on that object (Figure 4.3).

Figure 4.3. Objects Retain Each Other

The retain-count system gives the developer a lot of control over how and when objects are deallocated, but it requires that you meticulously retain and release objects. If you release an object too many times, it will be deallocated prematurely, and your program will crash. If you retain an object too many times, it will never get deallocated, and you will waste memory.

Fortunately, some simple rules govern when you should retain and release objects. These rules take the guesswork out of memory management with retain counts. In fact, once you are clear on these rules, it will be very clear in most cases when to retain and when to release. We’ll cover the rules later in this chapter.

Leak-Free Lottery


We didn’t give any thought to memory management in the lottery exercise we started in the previous chapter. Let’s open that project and fix it. Note that if you did not disable automatic reference counting at the end of the Chapter 3 be sure to make that build setting change now.

Open lottery.m. In main(), we had created an instance of NSDate:

NSDate *now = [[NSDate alloc] init];

After this line, now has a retain count of 1 because it was just allocated. Whenever we create a new instance of an object, we are taking responsibility for releasing it. Here’s how we would release now:

[now release];

We had also created instances of NSMutableArray and NSDateComponents. Open main.m and release these objects once we are done with them:

}

// Done with 'now' and 'weekComponents'

[now release];

[weekComponents release];

for (LotteryEntry *entryToPrint in array) {

NSLog(@"%@", entryToPrint);

}

// Done with 'array'

[array release];

}

return 0;

}

Now the objects will be properly deallocated before the process ends.

What about the LotteryEntry instances we added to the array?

// Create a new instance of LotteryEntry

LotteryEntry *newEntry = [[LotteryEntry alloc]

initWithEntryDate:iWeeksFromNow];

// Add the LotteryEntry object to the array

[array addObject:newEntry];

An array does not make a copy of an object when it is added. Instead, the array stores a pointer to the object and sends it the message retain. When the array is deallocated, the objects in the array are sent the message release. (Also, if an object is removed from an array, it is sent release.)

Let’s quickly go over the life of the LotteryEntry object in your application:

1. When the entry object is created, it has a retain count of 1.

2. When the entry object is added to the array, its retain count is incremented to 2.

3. When the array is deallocated, it releases the entry. This decrements the retain

Return Main Page Previous Page Next Page

®Online Book Reader