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.

Bringing memory leaks under control with Instruments<br />

281<br />

Additional schemes can be helpful<br />

Although the Xcode project templates produce a project with one scheme by default,<br />

you can add other schemes to your project.<br />

You could, for instance, create different schemes for “light” and “full” versions of<br />

your application. Using different C preprocessor defines, you could configure your<br />

source code with code similar to the following<br />

#ifdef FULL_VERSION<br />

...<br />

#endif<br />

to alter the behavior of the two application variants without needing to maintain two<br />

completely separate Xcode projects.<br />

other functions that need to be updated. Luckily, Xcode provides support for performing<br />

such tasks in a more efficient manner. From a terminal window, you can navigate<br />

to your project’s folder and use the following command:<br />

tops replace "NSLog" with "LogAlways" *.m<br />

This command replaces all calls to the NSLog function with calls to LogAlways. It performs<br />

this task for all *.m source code files found in the current directory. Tops is<br />

smart enough not to alter other references to NSLog, such as those in comments; it has<br />

built-in knowledge of <strong>Objective</strong>-C syntax and performs more than a simple search and<br />

replace. For safety purposes, if you want to double check what a call to tops will perform,<br />

add the -dont argument to the command line. This argument causes tops to<br />

perform as normal, except it won’t modify a single file. Instead, it sends to the console<br />

a list of changes it would have performed had the -dont argument not been present.<br />

It’s also possible to perform this task directly from Xcode’s text editor: move the<br />

mouse over a call to NSLog, right-click, and select Refactor. In the dialog box that<br />

appears, type in LogAlways and click the preview button. You’ll get a list of files Xcode<br />

will change, and selecting each one will graphically highlight the changes that will<br />

occur once you click the Apply button.<br />

That sums up improved the NSLog situation—but there’s another problem with the<br />

sample application. If you run the application, it appears to be working correctly, but<br />

with time (and especially on a real device), it will eventually crash. It has a memory<br />

leak, but how do you find it?<br />

14.3 Bringing memory leaks under control with Instruments<br />

In Xcode select Product > Profile. In addition to the iOS Simulator, you’ll see an application<br />

called Instruments. From Instruments’ initial screen, select the Leaks trace<br />

template and click Profile. A screen similar to figure 14.4 will be displayed.<br />

While Instruments is monitoring the execution of your application, select the Allocations<br />

instrument and scroll around the UITableView. Notice that the line labeled All<br />

Allocations (representing all memory allocated in the application) keeps indicating that

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

Saved successfully!

Ooh no, something went wrong!