Xcode 3.2 Daily Tip: Analyzing Your Code
More Xcode 3.2 tips for Mac and iPhone developers.The LLVM/Clang static analyzer bundled with the Snow Leopard developer tools automatically detects a variety of memory management bugs in Objective-C programs. It's a terrific tool for finding memory leaks and other issues and it is now easily available to all developers, both for the Macintosh and iPhone platforms.
I first learned about using the analyzer with iPhone projects from a blog post by Joe Heck of rhonabwy.com. Heck pointed out that the Intel-only analyzer worked with the Intel-based Simulator code generated by the iPhone SDK, letting you use the analyzer with your iPhone projects.
At that time, you had to download a copy of the analyzer, install it by hand, and run it from the command line. It was amazingly helpful but a bit of a pain to use.
No more. Xcode 3.2 incorporates the static analyzer tool directly into its IDE. Choose Build > Build and Analyze (Command-Shift-A) and the analyzer automatically checks your code. and presents any bugs detected by the analyzer. Static analysis evaluates source code to automatically find bugs, issuing hints that are similar in nature to compiler warnings but targeted at Foundation (Cocoa) and Core Foundation memory management.
Each bug is marked with a blue icon and a description. I do wish that the text didn't seem to "cut off" so abruptly. Resizing the Xcode editor window does not affect the hard right alignment of the bug reports. This bug refers to the local watcher variable, which is allocated and initialized but not released.

The tool is not perfect. It may flag nonexistent "bugs" in programs, so there are definitely false positive results that will show up as well as gray areas. In this example, the watcher is used until the application teardown, so the fact that it's leaking is not really a problem. That having been said, the analysis is amazingly helpful and if you do find real bugs, the Clang Static Analyzer team solicits bug reports.
To learn more about your bug, click the blue branch icon in the code itself. The analyzer offers a detailed view of the bug and its issues. This presentation provides more information about the specifics of the issue at hand.

In this detail view, clicking any single blue arrow opens the Build Results pane, showing the analyzer result list. Hide or show analyzer information by clicking the blue branch icon in the left gutter.
It's easy to overlook the new built-in static analysis feature of Xcode 3.2, but you'd be missing out on a great feature if you didn't explore it further.
Share
Source: http://clang-analyzer.llvm.org/
More Xcode 3.2 tips for Mac and iPhone developers. The LLVM/Clang static analyzer bundled with the Snow Leopard developer tools...
Add a Comment
XCode is really becoming more and more a truly awesome development tool. Now if I could only get a grasp on Objective-C and Cocoa...
September 09 2009 at 9:11 AM Report abuse Permalink rate up rate down ReplyWow, the static analyzer found 10 problems in my code, several of which have been causing major headaches for me by causing didReceiveMemoryWarning calls.
Of course, turns out the problem was that I forgot to release a modal view controller properly. Silly me!
I was used to use the Clang analyzer: it is an incredible tool that helps to detect memory leaks which are well hidden in the code and sometimes difficult to track. Besides it helped to understand the reason of the leaks, as in its report it shows the "logic steps" that allowed him to detect the leak.
The only limitation was that you had to setup a special target each time: now with the integration in Xcode 3.2 everyone will be able to run it without extra setup time.
The static analyzer *does* report false positives, but I wouldn't really consider this one. True, it's your main method, but good form would suggest that you should autorelease the watcher object â that would get rid of the reported "error" and work just fine, since the pool in that method isn't drained until CFRunLoop() returns control.
But that's beside the point. I love the static analyzer, and the fact that it's part of Xcode 3.2 is awesome. It should help improve the stability and quality of many an app, particularly because the types of bugs it excels at finding are so often the same type that humans fail to see. :-)
This sure beats running the command line analyzer which for some reason outputs html files to /var which you can't even view in the Finder. Ok you can go through the Go To Folder option but still, the command line one was a pain.
September 08 2009 at 2:09 PM Report abuse Permalink rate up rate down ReplyGreat tip and article!!!! Thanks
September 08 2009 at 1:49 PM Report abuse Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Verizon Leather Sleeve for Tablets for $4 + free shipping
- Wicked Jaw Breaker Noise-Isolating In-Ear Headphones for $6 + free shipping
- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



6 Comments