Now With 100% Fewer Memory Leaks!

This post is probably only of interest to other programmers, but hey, don’t let me stop you from reading…

I got back into the code today and I was trying to get multiple music tracks to queue up one after the other using the SoundEngine code. I was having some problems and so I went onto the developer forums and discovered that a lot of people have had problems with SoundEngine. Apparently it had a lot of memory leaks and other problems. Some of the leaks have fixes on the forums. However, what was most shocking was to discover that Apple has pulled all of the example SoundEngine code off the developer site. They’re basically telling people not to use it anymore.

With the latest public update to the SDK there are preferred ways to do audio. The problem is, they only work on devices running the 2.2 SDK. I want my game to run on anyone’s iPhone or iPod Touch, so I’m trying to support 2.0.

I managed to track down a bug in the SoundEngine code that was preventing more than one music track from playing and then decided it was time to boot up Instruments (Apple’s cool performance analyzer) to have a look for memory leaks. There were quite a few, all of them coming out of the fix I’d just put in place for the audio system. Ugh.

I spent the afternoon tracking stuff down and I’ve finally fixed all the leaks in the game (for now). I can also queue up multiple music tracks. Hooray!

I did want to post a couple of things I learned today about tracking down leaks with Instruments:

  • You need to launch your iPhone app directly from Instruments (you can’t track leaks in an already running iPhone process).
  • Once you find leaks, go to “View” -> “Extended Detail”. I can’t tell you how important this is! I spent an hour chatting with a friend online trying to find a way to get a full stack trace for the leaked memory allocation. It turns out it’s all in that little hidden window! When you open the Extended Detail window and click on a memory address that leaked, it will show you a full stack trace that lead to the leak.
  • Double-clicking on a function in the Extended Detail stack trace will open the source code to the exact line in XCode!
  • You can’t create custom DTrace instruments for use with an iPhone, they appear to only work with Mac OS X processes.

Now that I know a bit more about how to use it, I’m quite impressed with Instruments. It seems to be a very powerful tool.

Owen

2 Responses to “Now With 100% Fewer Memory Leaks!”

  1. dmitry says:

    This was useful thanks!
    “Extended view” needs to be way more prominent.

  2. OG says:

    I agree. :) I’m glad it helped.

Leave a Reply