1

我正在尝试为 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];
    }
}

4

2 回答 2

0

我发现了我的问题,我所有的内存泄漏都源于以下语句:

self.stories = [[NSMutableArray alloc] init];

这会导致故事的保留计数增加 2,因为 setter 在新分配的数组上调用保留。

我用这个替换了上面的语句,它解决了我的问题:

NSMutableArray *array = [[NSMutableArray alloc] init];
self.stories = array;
[array release];
于 2010-02-25T16:51:39.547 回答
0

修复代码的另一种方法是替换

self.stories = [NSMutableArray alloc] init];

self.stories = [NSMutableArray arrayWithCapacity:10];

arrayWithCapacity方法是自动释放的,所以你不需要手动调用 release。(对于其他类也是如此,例如 setWithCapacity、stringWithFormat 等)

谢谢,山姆


PS 对您的问题没有帮助,但这些行看起来有点不寻常:

[self.currentTitle release];

你可能应该这样做:

self.currentTitle = nil;

这将像您的代码一样释放 currentTitle ,但它也会将其设置为 nil ,这意味着您不能再次错误地使用它!

于 2010-04-19T11:16:27.943 回答