05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

486<br />

CHAPTER 26 MAC <strong>OS</strong> X DEVELOPMENT: OBJECTIVE-C<br />

NOTE This section discusses the traditional Objective-C model for manual memory management.<br />

Although it is still valid and useful information, <strong>Leopard</strong> introduces an alternative:<br />

automatic memory management, also known as garbage collection. You can read more about<br />

garbage collection in the “Objective-C 2.0” section.<br />

To ameliorate this risk, Objective-C uses a system called retain counting. Retain counting is<br />

extremely simple, at least in concept. Every object that subclasses NSObject has an instance variable<br />

called retainCount. When an object is created by alloc or copy, retainCount is initialized to 1.<br />

Anyone interested in the object sends it a retain message, which increments retainCount. Anyone<br />

no longer interested in the object sends it a release message. When retainCount reaches 0, the<br />

object is freed, as shown in Figure 26-2.<br />

Figure 26-1. The risk of malloc/free in object- Figure 26-2. Retain counting lets objects be<br />

oriented programming shared safely.<br />

If everyone remembers to retain and release at the appropriate times, this system works perfectly,<br />

except for one little problem. What happens if you create an object and then pass it to<br />

someone else, as illustrated in Figure 26-3? The caller can’t retain the object until after you<br />

return it. If you release it before you return it, the retain count will drop to zero, and the object<br />

will be freed. If you return it without releasing it, the retain count will never drop to zero, and<br />

the memory will leak!<br />

The solution to this problem is to let a third party retain the object, as shown in Figure 26-4.<br />

That way, the object will not be freed until both the caller and the third party release it. This is<br />

accomplished by sending the object an autorelease message. The system will then place the<br />

object into an autorelease pool. The autorelease pool will keep track of objects on request and<br />

then send them the release message at the bottom of the event loop.<br />

If everyone has released an object before the autorelease pool has released it, the end result<br />

is the same. The object will be freed when the autorelease pool releases it. However, if a lot of<br />

objects are created and released, as in a long loop, the programmer may need to create an inner<br />

autorelease pool and drain it manually. This is easily done; autorelease pools are objects that are<br />

instances of NSAutoreleasePool.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!