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.

180 CHAPTER 9 Memory management<br />

The advantages of a reference counting–based memory management scheme include<br />

the following:<br />

■<br />

■<br />

Minimal overhead—Relatively little runtime support is required to implement<br />

reference counting. This helps increase performance and reduce power consumption,<br />

both of which are important on a mobile device.<br />

Deterministic behavior—Because the developers are responsible for allocating<br />

and releasing objects, they also have explicit control over exactly when and<br />

where these events occur.<br />

The potential disadvantages include the following:<br />

■<br />

■<br />

Control implies responsibility—There’s a greater risk of the developer making<br />

a mistake that will lead to memory leaks or random application crashes and<br />

malfunctions.<br />

Additional overhead—Including the developer in the memory management process<br />

means there’s yet another item for developers to consider while developing<br />

their applications rather than focusing on the core task of what makes their<br />

application unique.<br />

Now that you’re familiar with the concepts involved in reference counting–based<br />

memory management, let’s look at how it’s implemented in <strong>Objective</strong>-C via the retain,<br />

release, and autorelease messages and how to avoid memory leaks or the dreaded<br />

"Program received signal: "EXC_BAD_ACCESS"." exception.<br />

9.2.1 Releasing an object<br />

When an owner of an object is finished using the object, it must tell the object that it<br />

wants to give up ownership. This process reduces the internal reference count of the<br />

object by one and is typically called releasing an object because the operation is performed<br />

by sending the object a message called release, as demonstrated here:<br />

NSMutableString *msg =<br />

[[NSMutableString alloc] initWithString:@"Hello, world!"];<br />

NSLog(@"The message is: %@", msg);<br />

[msg release];<br />

Because this code snippet creates the NSMutableString object (via the alloc message),<br />

it inherently owns the string and must explicitly give up ownership when the<br />

object becomes excess to requirements. This is achieved by the release message sent<br />

to the object on the last line.<br />

In this example, the release message also indirectly causes the <strong>Objective</strong>-C runtime<br />

to send the object a dealloc message to destroy the object. This occurs because<br />

the release message is releasing the last (and only) owner of the object. The object’s<br />

retain count returns to zero when its last owner is released. If the object has more<br />

than one owner, a call to release has no visible effect on the application, other than<br />

to reduce the retain count, leaving the object with one less owner.<br />

It’s important to note that once ownership of an object is given up, it’s generally<br />

not safe to refer to or utilize that object again from the current section of code, even if

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

Saved successfully!

Ooh no, something went wrong!