I am having real trouble with release and leaks. I have an array that just wont stop leaking! here is my code:
I have declared otherValuesArray in the .h
I have tried hundreds of different ways, including autorelease.
Can someone tell me what I am doing wrong?? Thanks
otherValuesArray = [[NSMutableArray array] retain]; //89% leak
NSString *tempString;
tempString = [[NSString stringWithFormat:@"%d",challengeID] autorelease];
[otherValuesArray addObject:[NSString stringWithString:tempString]]; // 11% leak
tempString=nil;
tempString = [[NSString stringWithFormat:@"%d",scoreMultiQuant] autorelease];
[otherValuesArray addObject:[NSString stringWithString:tempString]];
tempString=nil;
int challengeDoneTemp = [challenges otherValues:otherValuesArray];
tempString=nil;
[tempString release];
otherValuesArray = nil;
[otherValuesArray release];
Swap the last two lines. After setting
otherValuesArrayto nil, there is no point in sending it a release message. It is already nil, so release have no effect. So you are leaking that memory, as that is not released. Correct code will be,And also
stringWithFormatis already autoreleased. So you don’t need to send it an autorelease message. You are getting a leak for it as the containerotherValuesArrayis leaking.And also (though not related to leak) you don’t need tempString at all. You can do this in one line: