views:

56

answers:

2

Hello,

I am getting close to finishing the release of my application and are trying to use Instruments to fix any memory leaks.

How come that I can spot one memory leak when using Instruments and my device but not when I am using the iPhone simulator? I understand that this is a high-level question, but I don't think posting any code would help anyways (quite a bit of code...).

And is it possible to get instruments to point to the source code where it think the leak is? I can do it when using the simulator, but it seems like it doesn't work when using it for the device (objects are represented by the address (I assume) while running it for the simulator it sees what object it is, setup issues?)

Thanks in advance!

Regards, Niklas

Update: Could it have something to do with that OSX is having automatic garbage collection but iOS doesn't?

A: 

If you haven't already take a look at the handy "Build and Analyze" option in the build menu. It will run the static analyzer which generally does a great job. If nothing turns up with that you should could some time reviewing the WWDC session videos on Instruments.

There is no substitute for profiling on hardware and with the debugger and instruments connected you can get everything you would in a simulator context.

Nick
Thanks for you answer. I have done the Build and Analyze, it helped me a bit but also marked some "unnecessary" activities (like when allocating a new object varible inside an if clause where the actual variable is defined before the if clause. Then returning the object with "return [variable autorelease]". It will for sure be allocated before.).
Nicsoft
+2  A: 

Trust only the device. That's what your user will use to run your application.
Don't trust the simulator.
As a demonstration of this, I just intentionally added a leak to a project. The leak was not detected while in the simulator, but showed up as expected on the device.
The simulator is just that: a simulator. It can be useful to work faster, but is never a replacement of the device.

Once Instruments showed you a leaked object, you can double click on it. It will show the part of your code responsible for the leak. This works for the simulator and the device.
When you compile for the device, make sure you are in debug mode (and that the settings for this mode kept all your symbols).



Some more tip that you might find useful:

For a more fluid session, disable the "Automatic Leaks Checking", and manually press the "Check for Leaks Now" button when appropriate.

The "Build and Analyse" command will do a fantastic job to help you find leaks. It's not (totally) magical, so it won't find all leaks. For example, iVars leaked won't be identified. But for the scope of a method, it's just awesome.
I highly recommend to activate the "Run Static Analyser" flag in your build settings (or only for the Release mode if you have a slow to compile machine).

If you want more info about how to use Instruments to find leaks, read this Apple doc: Instruments User Guide: Built-in Instruments and Instruments User Guide: Viewing and Analysing Trace Data > Looking for Memory Leaks
You can also watch the video of the WWDC related sessions.
If you still don't understand where your leak come from, it's time to (re)read the Memory Management Programming Guide.

Thank you for wanting to ship a leak-free application. With iOS 4, it's now more important than ever.

Guillaume
Thanks for your answer! I used the Build and Analyse already, it helped me to find some leaks. But, as "too" often, you fix something and some other problem dissapears. I wast just cleaning the code (mainly NSLogs and comments) and then the memory leak was gone... And doesn't all people try to fix memory leaks...? (You don't have to answer that question... ;) )
Nicsoft