Skip to content

Lou Franco

These are the stories that have been posted by Lou Franco category.

Making Xcode's Build and Analyze much better


Published to LouFranco.com by Lou Franco August 04, 2010 15:30
A few weeks ago, I discovered that the Build and Analyze in Xcode didn’t find as many problems as scan-build did directly when you turn all options on.

On the Clang analyzer site, they have instructions for setting up the latest analyzer so that Xcode would use it. Unfortunately, the settings that find some very common problems are off by default. Here are instructions for getting the Clang analyzer to run with those options on.

To make sure you are doing it right, make your project completely succeed a Build and Analyze, then comment out a release that you are doing in any dealloc. Rerun Build and Analyze -- it should not find this problem.

Then,

1. If you don’t have it, download and untar
the latest checker
2. Make a script file called
full-analyze-clang and put it in the checker’s bin directory, with this code
#!/bin/bash
CLANG="`dirname $0`/clang"
CLANG_CMD="\"$CLANG\" -Xanalyzer -analyzer-check-objc-missing-dealloc -Xanalyzer -analyzer-check-objc-missing-dealloc -Xanalyzer -analyzer-experimental-internal-checks $*"
eval $CLANG_CMD

3. From the Terminal, run this command to make the script executable
chmod +x full-analyze-clang

4. In the checker root directory, there is a script called set-xcode-analyzer. Run it like this:
sudo ./set-xcode-analyzer --use-checker-build=FULL_PATH_TO_CHECKER/bin/full-analyze-clang
replacing FULL_PATH_TO_CHECKER with the path to the checker directory where you untarred it in step 1.

5. To test, Run the Build and Analyze on the project you set up with the bad dealloc. If it finds 0 errors, this could be because something is wrong with the way the script is set up.

Important parts if this stops working with a future clang:
The set-xcode-analyzer expects a directory or a full-path to clang. It checks to see it has a full-path by looking for a path ending with “clang”, which works for us because our script’s name ends in “clang”. If they change this script in the future, this might not work

Build and Analyze just reports a successful build if our script reports errors -- it’s just looking for the existence of result files. You might need to add logging lines to the script to figure out what’s going on. Log to a full-path because the current directory is inside Xcode’s path somewhere when the script is run.

Use something like this as your last line to debug:
eval $CLANG_CMD > `dirname $0`/cl.out 2> `dirname $0`/cl.err

Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

Xcode 4


Published to LouFranco.com by Lou Franco July 29, 2010 14:30
There are two great things about Xcode 4 that mean I had to download and try it right away
  1. It runs side-by-side on the same machine as Xcode 3.2
  2. It uses the exact same project file format
This hasn’t been the case in the last few major upgrades from what I remember, and I was really glad to read that in the release notes.

My first impressions after an hour or so:

Putting the view in Assistant mode (Opt-Cmd-Enter) is really nice. Shows the .m when editing the .h and vice versa. When editing an .xib file, it shows the ViewController.

Keeping the Interface Builder dialogs organized as parts of a single window is also a big win. It’s confusing having them as free dialogs. Specifically, having the parts of the main dialog (File’s Owner, First Responder, View, etc) as just part of the chrome (and always visible) is nice.

I think I’ll really like the Log Navigator -- especially for debugging. I am constantly clearing the console, but this saves each one in a separate view so I can keep them around.

The Project Navigator is so much cleaner (replaces the Groups and Files tree). It always felt like Xcode just stuffed everything there instead of organizing it elsewhere in the GUI. However, it took me a long time to figure out where Build Settings were (click the project name in the Project Navigator), but it seems obvious now.

Build and Analyze still doesn’t find
dealloc omissions of retained property releases. I would like the default to be set to finding these.

Some keyboard shortcut changes. The GUI is so radically different, so this is excusable. For the most part, things are the same.


Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

LGPL and the iPhone


Published to LouFranco.com by Lou Franco July 24, 2010 09:30
The entire point of the LGPL is to give closed source applications access to an open-source library in a way that maintains the freedom of the library. Specifically, the user of the application must be able to change the LGPL library and have the application use it.

To achieve this, the
LGPL gives you a few options for compliance. I am not a lawyer, but I believe that all of them are incompatible with iPhone apps delivered legally through the AppStore. I will lay out my reasoning here:

Section 4d gives two options for compliance: Either provide the source or object files suitable for relinking (4d0) or use a shared library (4d1).

Shared libraries are out, because Apple does not permit their use in the AppStore.

Section 4d0 is a little more complicated. For an application targeted to a PC or Mac, it would be easy to comply by just packaging up your .o files, the LGPL library, a makefile, and documentation. However there are subclauses in section 4 that make me believe that there is no way to comply on the iPhone. Specifically:

Section 4d0 not only specifies that you must convey the minimal source and object files, but you must also provide terms. Together they must “permit the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work”

Section 4e requires that you provide instructions that allows some to change the LGPL portions, create a new combined work and install and execute that combined work.

To do either of these things, the user would need to be enrolled in Apple’s developer program which costs $99/year. These terms are implicitly part of your terms, meaning that it’s basically equivalent to you charging $99/year for the user to make modifications.

The GPL allows you to charge what you want to convey the Combined Work to begin with, but section 10 of the GPL (which I think still applies to the LGPL) states:

You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License.

Now, obviously, being able to develop isn’t free, but those costs are not as directly related to exercising your rights as the $99/year is. Specifically, you cannot create an executable or put in on a device without the certificates you obtain from the program.

Here’s an alternative view from
Huy Zing -- I think the “Spirit of the LPGL” section in the blog is actually required by the text of the license.

Here’s a discussion on the cocos2d forum about
switching away from LGPL. They tried adding to the license at first and then moved to an MIT license.

I haven’t been able to find any official ruling on gnu.org, so I will write them and see if they can add to their FAQ (
as they did for Java when it was an issue).


Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

Understanding EXC_BAD_ACCESS


Published to LouFranco.com by Lou Franco July 22, 2010 15:00
A couple of days ago, I wrote about how to debug a crash that reports EXC_BAD_ACCESS. One thing I didn’t cover is what EXC_BAD_ACCESS means, which I’ll try to do now, as it will clear up a lot of the questions I’m getting about the previous blog.

The description here is a high-level way of thinking about it. The details are quite a bit more complicated, so I’m simplifying it.

On the iPhone (and most modern OS’s), your application is given memory as you need it. The memory is given in chunks that are bigger than your request, and then the unused parts are parceled out over the next few requests.

When you deallocate an object, that chunk can’t be returned to the OS right away. It has to wait until all of the memory in the chunk is deallocated.

Inside all of this memory is a complex data structure that is maintained by the alloc and dealloc messages to organize how each part of the allocated memory is being used. The pointers you hold are just part of that datastructure (where the object is), there are other parts that are only used by the allocator.

What EXC_BAD_ACCESS is saying is that you did something that caused a pointer (yours, one internal to the iPhone, or one that the allocator is using) to be dereferenced and that memory location isn’t inside one of the chunks assigned to your program.

This could be because
  1. The pointer used to point to memory that was ok, but its chunk was deallocated.
  2. The pointer is corrupt.
The line of code that your app crashes on is not the root cause of the problem. The problem in #1 is whatever line of code caused the premature deallocation, and the problem in #2 is whatever line of code corrupted the pointer.

Your goal in debugging this is to make the problem line of code be flagged by either the compiler or debugger.

If you do that, then fixing it becomes a lot easier.

Of the two possible problems, #1 is far easier to find. It’s almost definitely because you didn’t use retain/release correctly and there where either too many releases or too few retains.

Do this:
  1. Run Build and Analyze. Make sure you fix or understand every single error it flags. I personally have 0 Build and Analyze errors in every project I have and I go out of my way to keep it that way. If I ever get a false positive, I figure out how to make Build and Analyze understand what is going on, so that it doesn’t flag it.
  2. Run scan-build with all checks on. This isn’t built in, so if you’re in a hurry, skip this for now. scan-build is the project that Build and Analyze is based on. It can be run with much more thorough settings.
  3. Set up Xcode so that it never deallocates. Instead, it turns objects into Zombies that complain if they are used. See Tip #1 on this post for instructions.
If you have a clean Build and Analyze and no Zombies complain of being accessed, and you still get EXC_BAD_ACCESS, then it’s a good bet that you are not accessing deallocated memory. It’s not a sure bet, because the iPhone SDK gives you access to the C library which uses a different kind of allocation, which you could be using wrong.

For #2, your task is harder. If a pointer is corrupt, there are lots of possible reasons
  1. The pointer could have never been initialized.
  2. The pointer could have been accidentally written over because you overstepped the bounds of an array
  3. The pointer could be part of an object that was casted incorrectly, and then written to
  4. Any of the above could have corrupted a different pointer that now points at or near this pointer, and using that one corrupts this one (and so on)

If this is the situation you are in, then these are the things that will help
  1. Enable Guard Malloc (Tip #2) - this makes the datastructure that represents the allocations much more sensitive to corruption. You need to use the enhanced features of the debugger to get anything out of it (explained in the tip).
  2. The line of code that triggers the crash is a clue to what pointer is corrupt. If you move it around it might be able to help you narrow down the point of corruption.
  3. If all else fails and you are desperate, try Valgrind.

Once one of these methods gives you a different problem (either a warning or another EXC_BAD_ACCESS), don’t worry that it seems completely unrelated to your original problem -- that’s a common problem with corruption.

Also, random changes to your program may make this problem “go away” -- it’s not really fixed, though. Your corruption or early deallocation is still there, but it’s not triggering an EXC_BAD_ACCESS. Its effects could be far worse, however, so it’s a good idea to try to keep reproducing it until you are sure you addressed the problem.

Things to remember
  1. Corrupting a pointer doesn’t immediately trigger EXC_BAD_ACCESS. Neither does using a corrupted pointer unless it’s specifically now pointing to memory that isn’t mapped to your application.
  2. Deallocating an object doesn’t immediately release the memory to the operating system -- only when the chunk is unused, can it be returned.
  3. The line of code that is triggering the EXC_BAD_ACCESS might not be the problem. It can be a good clue, but don’t assume the problem is this code.

Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

How to Debug an iPhone App Crash, Part 1: EXC_BAD_ACCESS


Published to LouFranco.com by Lou Franco July 20, 2010 23:30
This is going to be a multi-part series on how to debug an iPhone crash.

This post is for how to debug crashes that show that the app received the signal EXC_BAD_ACCESS in the console. If you have this, the most likely thing that you are doing is sending a message to a released object. Another possibility is if you are using C/C++ allocations (or a library that uses it) and are overrunning memory or using freed memory.

Update: I explain this much better in a follow-up to this blog

Here are a few ways to debug that:

  1. Run Build and Analyze: This kind of Build is very good at finding retain/release bugs. Take a good look at everything it flags.
  2. Even better, run scan-build. I did a test recently, and found that some common errors are off by default in Build and Analyze that can be turned on in scan-build.
  3. Choose Run > Enable Guard Malloc in the menu, and then re-run your application. This finds a whole class of buffer overrun issues. If this detects it, you’ll see a better error in the console. Read this to see how to use Guard Malloc once it’s enabled.
  4. You can instruct the compiler to ignore release calls and then report if anyone is sending messages to objects that would have been deallocated. This results in much better errors in the Console if it detects them.
  5. Valgrind is the gold standard for finding memory bugs on Linux, and here’s a way to get it working in the iPhone Simulator.

Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

scan-build finds things Build and Analyze does not


Published to LouFranco.com by Lou Franco July 15, 2010 00:30
Ever since I started using the latest Xcode, I’ve loved having scan-build built in as Build and Analyze. I did feel like it was less thorough, but I didn’t have time to prove it -- now I have.

There is one kind of bug that scan-build was very good at finding -- forgetting to set retained properties to nil in dealloc isn’t found by default in Build and Analyze. It’s not even part of the default scan-build any more (not sure why -- it was great).

Anyway, I recommend using scan-build directly instead of Build and Analyze. Here’s how:
  1. Download scan-build
  2. Just unpack it into any directory and add that directory to your PATH
  3. Open a Terminal and cd to your project’s root directory
  4. Run scan-build and view results

Here is a scan-build line that works for iOS 4.0:

scan-build -analyzer-check-objc-missing-dealloc -analyzer-check-llvm-conventions --experimental-checks -k -V -o scan-reports xcodebuild -configuration Debug -sdk iphonesimulator4.0 clean build

I’ve turned on all checks, and now it catches if you forget to set properties to nil in dealloc.

Run Build and Analyze often in Xcode, but every once and a while, run a full scan-build with the latest version as it finds problems that the Xcode version will not.

UPDATE: Instructions on getting this working in Xcode from the Build and Analyze menu

Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

Trainers for Programmers


Published to LouFranco.com by Lou Franco May 11, 2010 21:30
Anyone who is interested in teaching programming to novices or non-programmers should check out what Zed Shaw is doing:

I've had an idea for an introductory book on programming that follows the model of "trainer" books for learning a musical instrument. Most of these books are organized like this:

  1. There’s a bunch of exercises.
  2. Each exercise is 1 or 2 pages.
  3. There’s only a little bit of prose.
  4. You do each exercise exactly, then move on.


The results are at Learn Python the Hard Way.

Favorite iPad apps


Published to LouFranco.com by Lou Franco April 06, 2010 22:30
Here are some of my favorite iPad apps so far:

WordPress: If you have a WordPress site, this is pretty essential. The iPhone version is pretty good too, but obviously the iPad is perfect for this. I’ve found the WordPress admin to be wonky on Mobile Safari, so this is the best way to edit a WordPress site on iPhone OS.

MaxJournal: This app is a great example of an iPad app -- it does one thing and it does it well. It looks great, has elegant date navigation, and strips a journal down to the bare bones. It needs a password feature, but apparently, the feature is done and waiting for approval. The developer is very responsive to feedback, which is a good sign.

Kindle: Kind of obvious. The advantage over iBooks is that the books you buy are available on the iPhone or your Mac or PC (some books have a limit, I hit that with one of my books)

Tweetdeck: Free and supports lists and other custom filters, which are essential for reading Twitter.

NewsRack: RSS reader that syncs with Google Reader. Well done, stable, with some nice touches.

And the disappointments:

CraigPhone: This is an iPad version of Craigslist. Tons of bugs. The app has an apology right on it (they put it in the AppStore without testing it on a real device). It’s free, so I guess that’s ok. Something about the interface just feels wrong though -- I suspect that they are using HTML views with some JavaScript drawing parts of the UI.

Tweetdeck: It’s the only usable twitter client for me, but it is crashtastic. Tweet-boom, tweet-boom -- move a column, buh-bye. Anyway, it’s free and I assume that they are working on it -- they need to check out my post about
debugging memory crashes on the iPhone and it probably wouldn’t hurt to run a Build and Analyze once and a while.

NYT Editor’s Choice: First, you can’t find this app by searching the store for “NYTimes” or “NY Times” because they named it with NYT. And, you don’t get the whole paper? But, I do if I go to the website? I don’t get that -- just charge me.

No Google apps or Facebook: Pretty surprised that these big players with great apps are absent from the AppStore on day one. Google made GMail work great on an iPad, though -- if they had done the same with Reader, I might not have bought an app.

Amazon KDK Beta Program is Open for Applications


Published to LouFranco.com by Lou Franco February 08, 2010 21:30
Amazon has opened their KDK Beta program. Amazon also published a KDK FAQ. As suspected, it’s Java ME based with a custom API for the Kindle:

[It will] provide UI components, JSON and XML parsers, HTTP and HTTPS networking, secure storage, and other features. Other APIs like audio and dictionary access will be available in a future release of the KDK.

Development will be possible in an Java environment, but it appears that Eclipse will be supported more directly.

What the iPad needs to be your only computer


Published to LouFranco.com by Lou Franco February 02, 2010 22:00
There’s been a lot of talk about how the iPad will be used by people that aren’t experts in computers. This article makes a good case that Grandmas and Technophobes will love the iPad:

The darndest thing happened in the last five days and I was fortunate to be privy to it. Apple has gotten people excited about computing. But this time, it’s not nerds or geeks and certainly not IT industry analysts. It’s everyone else. I had a curious set of three conversations this week. One with a grandma, one with a technophobe and the third with a self-proclaimed luddite.

And this article explains the difference in computing habits between the Gen Xers that came of age during the PC revolution and the generation before and after us.

The reason I’m starting to think the Old World is ultimately doomed is because we are bracketed on both sides by the New World, and those people being born today, post-iPhone and post-iPad, will never know (and probably not care) about how things used to work. Just as nobody today cares about floppies, and nobody has to care about manual transmissions if they don’t want to.

Both of these articles make a great case, and although, I need a regular computer to do development, I would like everything else to migrate to an iPad.

I started to use Macs again after ten years when I got a digital camera. I read the instructions for the Windows software it came with and then just plugged it into a Mac I inherited -- it just worked. Since then, I’ve used a Mac for everything except my job (.NET SDK development).

Which brings me to my first problem with the iPad -- how do I connect my digital camera? Steve Jobs talked about how great the iPad would be for photos, but how do they get on the iPad to begin with? Undoubtedly, this will be solved, but it’s a problem that will make the iPad useless for a lot of people if this is their only computer.

Secondly, I don’t take a lot of pictures and almost no video and I have about 10-15 GB of photos (and 20-25 GB of music). 64 GB is just completely inadequate if this is going to be the only place to store them. For the iPhone, synching gives me downsampled photos and music if I want to save space, but I can’t do that to my primary copy. I’m sure iPads will get to bigger sizes, but I’m also sure that we’ll need more too.

Even if the iPad had a terabyte, I really need some off device storage -- iPads can be lost, which would be bad, but not as bad as losing all of my data. It’s unclear how the iPad can be backed up if it’s your only computer.

I know that these problems will be solved eventually, but right now -- it’s not really possible for the iPad to work without a host computer -- if it’s like the iPhone, it depends on synching too much.

I’m not sure what the solution will be, but I have a Time Capsule -- if the iPad had Time Machine, and if the Time Capsule could suck the images off my digital camera (perhaps using the iPad as the UI), then I’d be most of the way there.