Wednesday, April 29, 2009

Static Code Analysis with 'clang'

On Leopard, all Cocoa objects are garbage collected, the developer has the luxury of not having to worry about memory management and can let the system take care of it. There is no such luxury on iPhone since garbage collection can be a performance intensive process. So, it is possible for memory leaks (and other nasty bugs to creep into the code during development). There are a bunch of tools at the developer's disposal to identify such problems and fix them. One such tool is the 'clang' analyzer.

I have found static code analysis to be an indispensable tool when it comes to discovering some very common bugs early in the development cycle. I first heard of the LLVM/Clang static code analyzer command line tool for Mac/iPhone late last year but started using it only recently.

The best way to understand how this works is to look at an example. The following is a sample code containing some obvious (and some not so obvious) memory leaks and bugs:

- (void)viewDidLoad {
// Uninitialized object
NSString *string1;

if (NO) {
string1 = [NSString stringWithString:@"Foo"];
}

NSUInteger counter = 0;

NSNumber *number = [[NSNumber alloc] initWithInt:0];
if (YES) {
// Memory leak
return;
}
[number release];
}

- (IBAction)createString:(id)sender {
// Memory leak
NSString *foo = [[NSString alloc] initWithString:@"Foo"];
}

- (NSString *)dummy {
NSString *string2;
return string2; // Returning an uninitialized object.
}

The analyzer can now be invoked simply by executing the following from the command line:
scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator2.2.1
scan-build is the script for running the analyzer over a project build. The above example assumes that scan-build is in the path. The '-k' flag forces the script to keep going even if there are problems in compiling some source files and '-V' flag runs a small web server and opens up the results file in a browser. Running the analyzer against the above code, detected 5 bugs.







The analyzer works best with 'Debug' builds. The tool is not perfect and does throw up false positives, but in most cases it is bang on target. The tool also does not catch some slightly more difficult to detect leaks, but more about that in another post.

LLVM/Clang Mac binaries are available on the home page.

Saturday, April 25, 2009

Why So Serious?

I decided to start posting all the non-tech related, fun stuff to my new blog. Keep checking it out for some interesting posts :-).

Sunday, April 19, 2009

Tuesday, April 7, 2009

Getting started with iPhone development

I have been doing serious development for iPhone for a little over three months now. I had experience programming Objective-C, but the start to iPhone development was not as easy as I had originally thought. I was never good at UI programming and generally shied away from it.

Until a few months ago, the only source of information for iPhone development was the official SDK documentation and the sample code. Moreover, all developers had to accept an NDA agreement which among other things prohibited discussing or sharing information related to iPhone development. However, in October 2008, Apple lifted the NDA and within a month or so books on iPhone development started hitting the shelves.

The two books that really helped me understand the iPhone SDK are Beginning iPhone Development: Exploring the iPhone SDK by Dave Mark and Jeff LaMarche and
The iPhone Developer's Cookbook: Building Applications with the iPhone SDK (Developer's Library) by Erica Sadun.



"Beginning iPhone Development", as the reviews show, is an excellent introduction to iPhone development. The book is well laid out and the book logically progresses from writing a simple "Hello World" application to taking advantage of the iPhone hardware. I don't think I would have been as comfortable with iPhone development, as I am now, without this book. But (there is always a but), the book does not cover any topics beyond the basics. This is understandable because the book is meant as an introduction, but they do not cover controls like UIScrollView or UIPageControl.




In contrast, "The iPhone Developer's Cookbook" is great at covering some not so basic topics. There are some good examples on animation, custom controls and undocumented APIs like cover flow.

Neither of these books cover advanced topics like memory management, debugging, optimizing with Instruments etc.

I am sure we will see newer editions of these books coming out sometime later this year and I hope they get better. I highly recommend getting both the books if you are considering serious iPhone development.

P.S.: Check this out. I just subscribed to it and I am hoping it covers some advanced topics.