Finding Memory Leaks in Cocoa + Objective-C

I found and fixed a memory leak in Poker Copilot’s HUD this afternoon. That’s the satisfying way I like Fridays to end.

A small component of Poker Copilot is written in Objective-C and Cocoa, the standard tools for Mac OS X programs. (The rest of Poker Copilot is written in Java). In Java, all the memory is cleaned up automatically using Java’s absolutely amazing and astounding garbage collector. For the first few years of Java, its garbage collector was primitive and caused long-running Java programs to freeze for some seconds from time to time. These days, however, the Java garbage collector is a programming work of art, working seamlessly, efficiently, and – most important of all – invisibly.

I’ve got used to that world. So moving into Objective-C, where I have to manually make sure memory is released seems like a jump back a decade or two in time. There is a garbage collector in Objective-C, but it produces code for Mac OS X 10.5+. When I tried to move Poker Copilot away from OS X 10.4 (Tiger) I had the virtual equivalent of an angry mob outside the front door, demanding justice for all. Well, for all the members of the angry mob!


And so, the Poker Copilot HUD had a memory leak. With the help of Xcode, Instruments, and this excellent blog post, I was able to find the leak and banish it forever. And hold of the angry mob.

Now back to the nice world of Java.