我分离了一个调用我的方法的线程,它有一个while循环。即使我将它们标记为autoreleasepool
,我还是手动释放对象,因为 while 循环可以继续运行一段时间。
问题是一段时间后,应用程序由于内存问题而崩溃。如果我查看 Instruments,我可以看到分配了一大堆 NSString,并在图中创建了通往天堂的阶梯。我没有释放什么?
while (keepGettingScores)
{
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSDictionary *json = [jsonString JSONValue];
[jsonString release];
NSMutableArray *scores = [[NSMutableArray alloc] init];
[scores setArray:(NSMutableArray*)[[jsonString JSONValue] objectForKey:@"scores"]];
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"totalScore" ascending:NO];
[scores sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
[sorter release];
[self performSelectorOnMainThread:@selector(updatePlayerTable:) withObject:scores waitUntilDone:NO];
[scores release];
[NSThread sleepForTimeInterval:1.0];
}