Finding bugs automatically

A colleague pointed me at a tool called LLVM/Clang static analyzer which is supposed to find bugs in code automatically. I was skeptical at first as how could it do it? Well, after running it on a few projects, I quickly became a believer! While it finds some stuff that isn’t technically bugs, it did catch a number of memory leaks that I’m actually ashamed to have found in my code. Some of my old crufty code had leaks in it and a few places where I misunderstood memory allocation caused me to revisit them (turns out that most of the sample code I’ve found dealing with NSManagedObject subclasses also has memory leaks). I’m going to make sure to run this on all my projects; I need to put a better front end on it so I don’t always have to type in the command and remember the syntax.

It took me awhile to get it to say that some of my projects were “bug free”; it uses function names as indicators as what should retain memory and what should not. There was a function GetSomeValue which returned a CFStringRef that had to be released by the caller. CF convention says that the caller does not have to release an object if the function name has get in it. I had to switch the name of the function to CopySomeValue as CF convention says that the caller must release an object that has copy or create in it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.