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.

286 CHAPTER 14 Debugging techniques<br />

Figure 14.6 An error detected by the static code analysis tool can be displayed<br />

graphically in the Xcode IDE. Notice how the execution path required to trigger the<br />

error is visually demonstrated by arrows overlaid on the original source code.<br />

Double-clicking anywhere on the row associated with tableView:didSelectRowAt-<br />

IndexPath: will bring up the source code, indicating where you attempted to access<br />

an object that no longer existed.<br />

In this case, you can see the error occurred because the string object pointed to by<br />

the mymsg variable in the viewDidLoad method had no explicit retain message. The<br />

following source code would resolve the problem:<br />

mymsg = [[self generateMessage:10] retain];<br />

This bug was engineered to be easy to detect; listing 14.3 contains other bugs. Luckily,<br />

Xcode is smart enough to detect some of them without your even running the<br />

application and waiting for it to crash! To get Xcode to perform a static analysis of<br />

your source code, select Product > Analyze. Notice that the return statement in the<br />

generateMessage: method is highlighted with the message “Undefined or garbage<br />

value returned to caller.” This message indicates that in at least one scenario, the<br />

generateMessage: method may fail to return a valid string object and instead return<br />

some random “garbage” that’s likely to crash the application. To find how this is possible,<br />

click the error message on the right-hand side of the Code Editor window. The<br />

blue arrows on top of the source code guide you through interpreting the error, as<br />

shown in figure 14.6.<br />

Notice in the figure that the variable newMessage isn’t assigned an initial value.<br />

Based on the value of x, the first if statement could evaluate to false, which would<br />

cause execution to jump to the second if statement. This statement could also evaluate<br />

to false, in which case execution would proceed to the return statement. At that<br />

point, an attempt to return the value of newMessage, the value of which isn’t explicitly<br />

set, will result in an error.<br />

When the code analysis feature of Xcode detects errors such as this, it’s a cue for<br />

you to analyze your logic and determine if there’s a corner case you haven’t covered<br />

or if a variable has accidentally been left uninitialized.<br />

14.5 Summary<br />

Xcode provides an extensive range of debugging tools and integrates well with a number<br />

of open source tools. The technology behind Instruments is mostly open source,

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

Saved successfully!

Ooh no, something went wrong!