Iphone: How to solve NSArray memory Leak?

The problem is your instance variables. In your init you are correctly assigning them to copies of the strings from the array. However, you need t also release them in dealloc (void) dealloc { zoneName release; lsdName release; standLat release; standLong release; super dealloc; } Now, you may be asking why the leaks tool is telling you the leaks are where you create the NSArray with the strings in it instead of the init method.

The reason is that copy for immutable objects is optimised to do nothing except send retain to self So those copies you have as instance variables are in reality the same objects as was created by componentsSeparatedByString.

The problem is your instance variables. In your -init, you are correctly assigning them to copies of the strings from the array. However, you need t also release them in -dealloc.

-(void) dealloc { zoneName release; lsdName release; standLat release; standLong release; super dealloc; } Now, you may be asking why the leaks tool is telling you the leaks are where you create the NSArray with the strings in it instead of the init method. The reason is that -copy for immutable objects is optimised to do nothing except send retain to self. So those copies you have as instance variables are in reality the same objects as was created by -componentsSeparatedByString.

ComponentsSeparatedByString: returns an autoreleased NSArray. You are not supposed to release that yourself, but the closest NSAutoreleasePool will do that for you. In line 61 you are overreleasing the array.

If you are concerned about the memory usage while performing the loop you can clear autoreleased objects in each iteration of the loop: for (...) { NSAutoreleasePool *pool = NSAutoreleasePool alloc init; // your loop contents. Pool drain; }.

1 good spot on the over-release. I missed that. – JeremyP Oct 11 at 13:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions