Instruments is informing me that I am leaking memory from the following code snippet
for (int x=0; x<20; x++)
{
LeagueTableRow *aLTR = [[LeagueTableRow alloc] init]; //leaks a certain percentage here
match = [substring rangeOfString: @"text team large-link"];
if (match.location == NSNotFound)
{
}
if ((match.location + match.length) > ([substring length]))
{
}
substring = [substring substringFromIndex:(match.location + match.length)];
match2 = [substring rangeOfString: @"title="];
substring = [substring substringFromIndex:(match2.location + match2.length+1)];
match2 = [substring rangeOfString: @">"];
aLTR.teamName = [substring substringToIndex:match2.location-1]; //leaks a percentage here
match2 = [substring rangeOfString: @"number total mp"];
substring = [substring substringFromIndex:(match2.location + match2.length+1)];
match = [substring rangeOfString: @">"];
match2 = [substring rangeOfString: @"<"];
aLTR.played = [substring substringWithRange:NSMakeRange(match.location+1, (match2.location-(match.location+1)))]; //leaks a percentage here
[self.theLeagueTableArray addObject:aLTR];
[aLTR release];
}
The instruments informs me of the leak and where I commented the code saying there is a leak it says that this line is a cause of a certain percentage of the leak.
The LeagueTableRow class is a very simple container class and each of the vars in it are released at the dealloc for the class.
What could be the issue here?
I alloc aLTR at the start of the loop and then release aLTR at the end of the loop. The 开发者_JAVA百科NSString methods are all autorelease on the returned strings AFAIK so the code seems tight.
Anybody able to shed some light on this confusing situation ? The build and analyze tool doesnt mention any issues here either.
Many Thanks, -Code
[self.theLeagueTableArray addObject:aLTR];
if the backing instnce variable for this theLeagueTableArray
is never released, neither will any of the league table rows in it. Check your dealloc method for this class and make sure the variable is released there.
Instruments can not show you where a leak actually occur, only where the leaked memory was allocated.
In your case the second leak, where the assignment to the property is done, is most likely only a consequence of the original leak.
The original leak is the LeagueTableRow
instances marked at your first comment.
This particular code looks ok as is, I would go and check how the theLeagueTableArray
property handles it's memory. Is it releasing memory when it should, is the class where this is implemented releasing the backing store for this property as it should?
In my experience the reason for leaks aren't normally the alloc call. What you'll find is that your passing this LeagueTableRow off to something else and you've forgot to release it.
Look through your code everywhere an instance of LeagueTableRow is retained, it's not being released somewhere
精彩评论