01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Autorelease pools<br />

185<br />

Ideally, you’d have a way to signal to <strong>Objective</strong>-C that you’re ready for ownership of<br />

an object to be released, but instead of relinquishing ownership immediately, you prefer<br />

it to be performed at some point in the future, once the caller has had a chance to<br />

claim ownership for itself. The answer to this scenario is called an autorelease pool.<br />

To use an autorelease pool, you simply send the object an autorelease message<br />

instead of the more immediate release message. This technique is demonstrated by<br />

the following version of the CreateMessageForPerson method:<br />

- (NSString *)CreateMessageForPerson:(NSString *)name<br />

{<br />

NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@", name];<br />

[msg autorelease];<br />

return msg;<br />

}<br />

With the string added to the autorelease pool, you’ve relinquished ownership of the<br />

string, yet it’s safe to continue referring to and accessing it until some point in the future<br />

when it’ll be sent a more traditional release message on your behalf. But what exactly<br />

is an autorelease pool, and when will the string be deallocated?<br />

9.3.1 What is an autorelease pool?<br />

An autorelease pool is simply an instance of the NSAutoreleasePool class. As your application<br />

runs, ownership of objects can be transferred to the most current autorelease<br />

pool. When an object is added to an autorelease pool, it takes over ownership of the<br />

object and sends the object a release message only when the pool itself is deallocated.<br />

This can be handy when you want to create a bunch of temporary objects. Instead<br />

of manually keeping track of each retain and release message you send, you can<br />

simply use the objects, safe in the knowledge that they will all eventually be released.<br />

9.3.2 Adding objects to the autorelease pool<br />

Adding an object to the current autorelease pool is easy. You simply send the object an<br />

autorelease message instead of a release:<br />

[myObject autorelease];<br />

This causes the current autorelease pool to retain the object after relinquishing the<br />

current owner of its ownership responsibilities.<br />

9.3.3 Creating a new autorelease pool<br />

In general, it’s possible to use the services of an autorelease pool without having to<br />

explicitly create one. This is because Cocoa Touch creates pools behind the scenes for<br />

your use. But it’s important to at least know when and how these pools are created so<br />

you know when objects will eventually be deallocated and how much “garbage” may<br />

accumulate beforehand.<br />

To create a new autorelease pool, you simply create an instance of the NSAutorelease-<br />

Pool class:<br />

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];

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

Saved successfully!

Ooh no, something went wrong!