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.

196 CHAPTER 9 Memory management<br />

view". If you refer back to chapter 1, you’ll notice that the CoinTossViewController<br />

class implemented viewDidUnload as follows:<br />

- (void)viewDidUnload {<br />

self.status = nil;<br />

self.result = nil;<br />

}<br />

These statements, which were not discussed at the time, ensure that when the view<br />

controller receives a didReceiveMemoryWarning message and decides to release the<br />

view, the two UILabel controls are also released. As a rule of thumb, any subview or<br />

control that’s retained by an ivar or property in the view controller class should be<br />

released whenever the viewDidUnload message is received.<br />

Correct memory management requires careful attention to detail<br />

As mentioned in the sidebar “Danger, Will Robinson, here be sleeping dragons” in<br />

chapter 5, it’s important to note the difference between the following three statements:<br />

[status release]; status = nil; self.status = nil;<br />

The first statement sends the object pointed to via the status instance variable a<br />

release message, hopefully freeing its associated resources. The statement doesn’t<br />

alter the value of the status variable, leaving it to point to the now nonexistent object.<br />

The second statement attempts to resolve this by setting the variable to nil. Doing<br />

this doesn’t send the object a release message, and hence the UILabel control will<br />

never be deallocated even though you’ve lost the ability to refer to it.<br />

The last statement (and its logical equivalent [self setStatus:nil]) performs<br />

both tasks by invoking the setter method associated with the status property. From<br />

chapter 5, remember that a property with the retain attribute specified automatically<br />

sends a release message to its old value when the property is being set to a<br />

new value, such as the nil specified here.<br />

Knowing that a UIViewController could decide to deallocate its view and re-create it<br />

when next required can have important implications for where you can place initialization<br />

logic or store application state. Get it wrong, and the application may appear<br />

to behave correctly during development, only to fail the first time your customer sees<br />

a low-memory situation develop.<br />

To see this process in action, create a new iPhone application using the Tab Bar<br />

Application template and uncomment the source code for the viewDidLoad method<br />

within the FirstViewController class. Then add a breakpoint to the beginning of the<br />

viewDidLoad and viewDidUnload methods.<br />

When you launch the application, you should see that viewDidLoad is immediately<br />

called to initialize the UI controls loaded from the FirstView NIB file.<br />

If you switch to the second tab and then return to the first, you shouldn’t notice<br />

any additional breakpoints being hit. This is because the UIKit framework keeps the

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

Saved successfully!

Ooh no, something went wrong!