Dan Wood: The Eponymous Weblog (Archives)

Dan Wood Dan Wood is co-owner of Karelia Software, creating programs for the Macintosh computer. He is the father of two kids, lives in the Bay Area of California USA, and prefers bicycles to cars. This site is his older weblog, which mostly covers geeky topics like Macs and Mac Programming. Go visit the current blog here.

Useful Tidbits and Egotistical Musings from Dan Wood

Categories: Business · Mac OS X · Cocoa Programming · General · All Categories

Tue, 06 Feb 2007

Leak Detection Tests

I was hearing about some memory problems with the CIImage to NSImage conversion I was doing. Since I was looking for leaks in just a small area, I came up with a basic technique that I wanted to share.

Essentially what I thought I'd do is to see how a particular method invocation grew memory over repeated use. That way, leaks would be easy to spot with ObjectAlloc (or probably with OmniObjectMeter).

So the basic technique was to just launch the application and then, in an infinite loop, do my processing. Each invocation was wrapped with an NSAutoreleasePool so that the expected behavior is that at the end of each loop, no extra memory has been allocated. I find that this technique, of repeating the process and watching the memory grow, is much easier to deal with than just trying to do an operation once and try to determine what changed.

while (YES)
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  [pool release];

Then you run the test from ObjectAlloc, and set a mark once the loop enters. If you see memory growing, then you have a leak.

Interestingly, I did find some leakage in my core image routines. It wasn't in my code, though! At some point, I need to isolate this and report it as a bug to Apple.

For fellow Cocoa developer bloggers: What are some debugging techniques you have come up with?