views:

74

answers:

2

Hi

I'm currently hunting down a memory leak in my app for iPhone. I'm using Instruments to track down the code that is causing the leak (becoming more and more a friend of Instruments!). Now Instruments show two lines: one in dark blue (row 146) and one in a lighter blue (150). From some trial and error I get that they are connected somehow, but not good enough at Objective-C and Memory Management yet to really understand how.

Does anyone know why different colors are used and what could be my problem?

I have tried to release numberForArray but the the app crashes when showing the last line in a picker view.

All ideas appreciated!

alt text

(Posting this I also realize that line 139 is redundant! Se there, already an improvement ;-)

+3  A: 

Ok, lets take a look at the object allocation/ownership behavior of this code...

numberForArray is assigned the result of -NSString stringWithFormat:, which is an auto-released object. That means that you do not want to release it (as you discovered).

That object is then added to the glucoseLoader NSMutableArray, which will retain it. You loop 100 times, creating 100 objects and adding them to glucoseLoader. When glucoseLoader is released, at line 154, it will also release all the objects added to it.

But wait, there's more: firstComponentRange is created from glucoseLoader using -NSArray initWithArray:. When you do that, all the elements of the source array are added to the destination, which will retain them again.

So, when/how do you release firstComponentRange?

David Gelhar
Thanks David for the informative and correct answer. I was going crazy for a while with this, missing (forgot) the thing about the autorelease.I use the firstComponentRange in a picker, so now I release that array in viewWillDisappear. Thanks again!
Structurer
A: 

Instruments is telling you that firstComponentRange is not being released (a small leak). Since the array retains its contents, you ate thus also leaking 100 NSString instances, allocated at the line indicated with a darker band (a more significant leak).

Barry Wark
Hi BarryThanks. I was going on that line of thought, which unfortunately led me to look at the problems in the wrong order. As you can see in my reply to David, the real issue was with my firstComponentRange. I'm now leak free :-)But I guess thats how it is. You learn by doing...
Structurer