views:

168

answers:

1

Hi all, I am in memory-leak cleanup mode on my latest app, and have come across something that I am unable to solve.

the following method has been cleaned up except for 1 nagging issue. Instruments tells me that my NSMutableArray called itemsToKeep is leaking memory, at the point that I am creating the object. Any ideas on why I am leaking memory would be most appreciated.

Here are some notes on retainCounts: entering the method: self.myList has retainCount = 1 exiting the method: self.myList has retainCount = 2 and itemsToKeep has retainCount= 2. I can easily do a [itemsToKeep release] at the end which brings both counts down to 1, but the app crashes after a while (and I think I know why).

Does anyone know how I can get rid of the memory leak for itemsToKeep?

Thanks.

-(void)parsedScores:(BOOL)shouldAdd {

//trim space, tab, newline from both ends
NSString *tmp = self.lblCurrentName.text;
NSString *list = [self trimString:tmp];

NSString *separators = @",";

[self.myList removeAllObjects]; // doesn't impact retain counts

self.myList = (NSMutableArray *)[list componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:separators]]; //this bumps up the self.myList retain count to 2

NSMutableArray *itemsToKeep = [NSMutableArray arrayWithCapacity:30]; 

for (NSString *item in self.myList) { 
 NSString *tmpItem = [self trimString:item];
 if (! [self shouldRemoveItem:tmpItem]) {
  [itemsToKeep addObject:tmpItem];
 } 
}

self.myList = itemsToKeep; //makes both variables' retain counts = 2

}

A: 

I can't see a leak in the method you've provided, so I assume it's happening elsewhere. You should check if self.myList is retained somewhere else without it being released.

Also, you probably shouldn't be looking at the retain count for debugging purposes. The retain count can be misleading because it doesn't matter how many times an object is retained as long as it's released an equal amount of times.

Tom Dalling
thanks... i'll check out self.myList - hadn't thought about that. it is normally added as a property and zapped in dealloc. But there might be somewhere else where it's retained. Also, thanks re: your point on retain count.
Ferris