01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

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.

184 CHAPTER 9 Memory management<br />

Now that you have a solid understanding of the retain and release messages, you<br />

can apply that knowledge to virtually any <strong>Objective</strong>-C or Cocoa Touch memory management<br />

technique. These two messages are the fundamental building blocks upon<br />

which all other techniques are ultimately placed.<br />

This isn’t to say that retain and release are the perfect memory management<br />

tools. Both messages have deficiencies or scenarios under which their usage can be<br />

painful. As an example, manual management of an object’s retain count can get laborious<br />

if an object is constantly passed among numerous owners. The next memory<br />

management technique we investigate builds on top of the retain and release foundations<br />

and is designed to ease such burdens.<br />

9.3 Autorelease pools<br />

The retain/release model has difficulties when ownership of an object needs to be<br />

transferred or handed off to something else. The code performing the transfer<br />

doesn’t want to continue retaining the object, but neither do you want the object to<br />

be destroyed before the handoff can take place.<br />

Consider the following method, which attempts to create a new NSString object<br />

and return it to the caller:<br />

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

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

return msg;<br />

}<br />

The first line of the CreateMessageForPerson: method allocates a new string object.<br />

Since the string is created via the alloc message, the method currently owns the created<br />

string. Once the method returns, however, it’ll never need to refer to the string<br />

again, so before returning, it should release its ownership.<br />

A temping solution may be to rework the CreateMessageForPerson method<br />

as follows:<br />

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

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

[msg release];<br />

return msg;<br />

}<br />

This solution, however, has the same bug as previously discussed. The first line creates<br />

a new string object with a retain count of 1, and the next decrements the retain count<br />

back to zero, causing the string to be deallocated. The pointer to the deallocated<br />

object is then returned.<br />

The problem with the retain/release model in this solution is that there’s no suitable<br />

location to place the call to release.<br />

If the call to release occurs in the CreateMessageForPerson method, you can’t pass<br />

the string back to the caller. On the other hand, if you don’t release the object in the<br />

method, you push the responsibility of freeing the string object onto every caller. This<br />

not only is error prone but breaks the principle of “match every retain with a release.”

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

Saved successfully!

Ooh no, something went wrong!