How to detect and fix memory leaks? (OSX, Xcode)

Hi -

I’ve got an app that seems to have a memory leak, but I have no idea how to pinpoint it.

As the app runs, I can watch the memory usage climb about 1MB/s. It just grows and grows.
I need to keep the project running for at least a full day since it will be installed in a gallery.

I took a look at Xcode’s Instruments profiler, but I’m not sure how to track down a leak in there.
I see under “All Heap Allocations” : total 2.12GB (101.9MB persistent).

I’m wondering if it might have something to do with AVFoundationPlayer? I’m running 4 layers of video (640x480 h.264).

Any advice where to go from here?
Thanks,
Tyler

I’m having a similar problem, and I suspect my ever-climbing memory usage has to do with OF’s video player. I’m not sure how to diagnose the memory leak either.

I did come across this thread in which Theo suggests loading videos into memory as an array of ofTextures. This may not be an option for you depending on the size of your videos. In any case, I’m at a loss for how to accomplish that exactly. I’m looking for some guidance on how to “cache” a video in this way.

The best way is indeed to use Instruments. I just had a massive memory leak in an app today and this is how I tracked it down:

First, you want to view the “Call Trees” in Instruments:

You will now get a list of memory allocations broken down by hierarchical function calls. To make your life easier, click on the Display Settings (right side of the Call Trees) and from there, choose “Invert Call Tree”.

Now the “heaviest” function will be at the top, and you can work your way back to see who called it from your code, which should then give you a good hint to find the culprit.

Hope that helps!

c.

2 Likes

Thanks so much! This is super helpful.

Interestingly, after trying again in 32bit mode, it seems like the memory leak is gone (or at least not as bad).
I haven’t let it run for hours to see if it eventually crashes (in either 64 or 32bit mode), but I do see the memory usage fluctuating in 32bit mode, whereas in 64bit, it only climbed.

I was under the impression that compiling in 32bit would revert to QT rather than AVF video player, but using Instruments (per cmendoza’s instructions), I still see calls to ofAVFoundationPlayer

If that’s correct is there a way to force oF to use QTKit?

Side note:
At first I wasn’t able to compile in 32bit mode because I guess I had a slightly older version of 0.9.3 that had a bug in the FreeImage library. After replacing [oF]/libs/FreeImage/ with the - [latest release files] (https://github.com/openframeworks/openFrameworks/tree/ffb8792277076e23a1b73b494463f61c9b1fb82b/libs/FreeImage) - I was able to compile.
See - Theo’s commits March 14-16 - for info on that issue.