0

I thought that I was really close to release this new App of mine when I ran into a dead end. My code works without memory leaks in the simulator (Xcode 4.0.2) but reports memory leaks on my devices.

I think my issue is related to that I copy an object, because in my troubleshooting attempts I tried without a copy, and then the memory leak goes away (but of course so do my functionality!).

What I do is that I add a number of instances of a subclass of UIView to an array . This subclass(Cities of which cityToAdd is an instance) has two UIViews and some variables that I need to access at a later stage.

If I do this I get memory leaks on my devices:

[arrayOfCities addObject:[[cityToAdd mutableCopy] autorelease]];

But if I do this I don't (but loose functionality)

[arrayOfCities addObject:cityToAdd];

In the subclass I have this to handle the copying:

- (id)mutableCopyWithZone:(NSZone *)zone{

Cities *newCity = [[Cities allocWithZone:zone] init];

[newCity initWithCityName:cityName 
                   onRing:ring 
             withTimeZone:timeZone 
       withTimeZoneOffset:timeZoneOffset 
             withDSTAngle:DSTAngle 
                   andDST:isDST];

 return newCity;

}

From Instruments I get this when testing on a device:

enter image description here

and when I drill down on the second row it shows this: enter image description here

Finally my initWithCityName method (sorry for such a long post!!) I put it as a picture to get the colors and messages from Instruments...

enter image description here

Finally the UIIMage imageNamedUniversal is an extension to give me @2x images on the iPad (but I have tried with the normal imageNamed and get the same memory leaks).

I dont know where to start!! Appreciate any ideas.

Thanks

Structurer
  • 694
  • 1
  • 10
  • 23
  • Just so you know, your app doesn't have to be leak free to make it through the review process - it just has to be free of the catastrophic ones. – Daniel G. Wilson Jun 01 '11 at 03:40
  • Looking over your code for *initWithCityName* and I notice you are calling retain or copy on your NSString that you are assigning to your ivars. – Black Frog Jun 01 '11 at 05:16

3 Answers3

0

Why are you calling two initialization methods? You are calling init and initWithCityName....

Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • The "double" init is because the first one is the "true" init of the instance, the other one is just to set parameters and load the UIImageViews. Maybe bad naming from my side? – Structurer Jun 01 '11 at 03:00
0

Two things to consider:

After you add cityView and DSTView as subviews, you can and should release them.

And you are initializing newCity twice in your copyWithZone.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
  • Hi Firoze. Simple things first. The "double" init is because the first one is the "true" init of the instance, the other one is just to set parameters and load the UIImageViews. Maybe bad naming from my side? Secondly, I tried to suggestion to release the subviews, and get the dreadful EXC_BAD_ACCESS. I guess because I access these subviews later on in the code. – Structurer Jun 01 '11 at 03:00
  • If you're getting EXC_BAD_ACCESS, then you must be over-releasing somewhere else? Unfortunately with memory mgmt issues you can have multiple mistakes and fixing one mistake just reveals a different one. But you have to clean that up to be leak-free. (and yes poor choice if names on the init. Why not just combine them and call [super init] from your initWithCityName... That seems to be the correct solution) – Firoze Lafeer Jun 01 '11 at 03:06
  • Hmmm, on second thought I see perhaps cityView is an ivar. So never mind then. But then are you releasing those ivars properly later? Are you replacing these two views later and forgetting to release them? – Firoze Lafeer Jun 01 '11 at 03:08
  • Hi Firoze, please see my own answer that I will add soon. Thanks for your help! – Structurer Jun 01 '11 at 03:17
0

Thanks for fast replies and suggestions. You got me on the right track. The cityToAdd that I added to my array was added several times in a loop, but I kept the alloc and init outside of the loop. Once I moved it inside the loop it works in both simulator and device.

Weird that the simulator don't report that memory leak though...

Again, thanks for your fast replies!

Structurer
  • 694
  • 1
  • 10
  • 23