我正在尝试为iPhone编写一个简单的RSS阅读器,它似乎工作正常,直到我开始使用Instruments,并发现我的应用程序泄漏了大量内存。
我正在使用NSXMLParser类来解析RSS提要。我的内存泄漏似乎源于重写的委托方法:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
和
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
我也怀疑从我的解析数据中填充单元格的代码,我已经包含了这些方法中的代码以及其他一些关键代码,我们将非常感谢任何见解。
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.currentElement isEqualToString:@"title"]) {
[self.currentTitle appendString:string];
} else if ([self.currentElement isEqualToString:@"link"]) {
[self.currentURL appendString:string];
} else if ([self.currentElement isEqualToString:@"description"]) {
[self.currentSummary appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
//asdf
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentURL forKey:@"URL"];
[item setObject:currentSummary forKey:@"summary"];
[self.currentTitle release];
[self.currentURL release];
[self.currentSummary release];
[self.stories addObject:item];
[item release];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
// Set up the cell
int index = [indexPath indexAtPosition: [indexPath length] - 1];
CGRect contentRect = CGRectMake(8.0, 4.0, 260, 20);
UILabel *textLabel = [[UILabel alloc] initWithFrame:contentRect];
if (self.currentLevel == 0) {
textLabel.text = [self.categories objectAtIndex: index];
} else {
textLabel.text = [[self.stories objectAtIndex: index] objectForKey:@"title"];
}
textLabel.textColor = [UIColor blackColor];
textLabel.font = [UIFont boldSystemFontOfSize:14];
[[cell contentView] addSubview: textLabel];
//[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[textLabel autorelease];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"]) {
self.currentTitle = [[NSMutableString alloc] init];
self.currentURL = [[NSMutableString alloc] init];
self.currentSummary = [[NSMutableString alloc] init];
}
if (currentElement != nil) {
[self.currentElement release];
}
self.currentElement = [elementName copy];
}
- (void)dealloc {
[currentElement release];
[currentTitle release];
[currentURL release];
[currentSummary release];
[currentDate release];
[stories release];
[rssParser release];
[storyTable release];
[super dealloc];
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
int index = [indexPath indexAtPosition: [indexPath length] - 1];
if (currentLevel == 1) {
StoryViewController *storyViewController = [[StoryViewController alloc] initWithURL:[[stories objectAtIndex: index] objectForKey:@"URL"] nibName:@"StoryViewController" bundle:nil];
[self.navigationController pushViewController:storyViewController animated:YES];
[storyViewController release];
} else {
RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rvController.currentLevel = currentLevel + 1;
rvController.rssIndex = index;
[self.navigationController pushViewController:rvController animated:YES];
[rvController release];
}
}
答案 0 :(得分:0)
我发现了我的问题,我的所有内存泄漏源于这句话:
self.stories = [[NSMutableArray alloc] init];
这导致故事的保留计数增加2,因为setter调用保留在新分配的数组上。
我用这个替换了上面的语句,它解决了我的问题:
NSMutableArray *array = [[NSMutableArray alloc] init];
self.stories = array;
[array release];
答案 1 :(得分:0)
修复代码的另一种方法是替换
self.stories = [NSMutableArray alloc] init];
与
self.stories = [NSMutableArray arrayWithCapacity:10];
arrayWithCapacity方法是自动释放的,因此您无需手动调用版本。 (这适用于其他类,即setWithCapacity,stringWithFormat等)
谢谢,Sam
PS没有帮助你的问题,但这些线看起来有点不寻常:
[self.currentTitle release];
你可能应该这样做:
self.currentTitle = nil;
这会释放currentTitle和你的代码一样,但它也会将它设置为nil,这意味着你不能再错误地使用它了!