Reputation: 25
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];
Upvotes: 0
Views: 145
Reputation: 104708
there's a number of memory management issues, as taskinoor pointed out.
here's a simplified form of the program:
NSMutableArray * tmp = [NSMutableArray new];
[tmp addObject:[NSString stringWithFormat:@"%d",challengeID]];
[tmp addObject:[NSString stringWithFormat:@"%d",scoreMultiQuant]];
assert(challenges);
int challengeDoneTemp = [challenges otherValues:tmp];
self.otherValuesArray = tmp;
[tmp release], tmp = 0;
Upvotes: 2
Reputation: 46037
Swap the last two lines. After setting otherValuesArray
to 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,
[otherValuesArray release]; // first release otherValuesArray = nil; // then set to nil
And also stringWithFormat
is already autoreleased. So you don't need to send it an autorelease message. You are getting a leak for it as the container otherValuesArray
is leaking.
And also (though not related to leak) you don't need tempString at all. You can do this in one line:
[otherValuesArray addObject:[NSString stringWithFormat:@"%d",challengeID]];
Upvotes: 6