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];

self.stories = [NSMutableArray arrayWithCapacity:10];

arrayWithCapacityメソッドは自動解放されるため、解放を手動で呼び出す必要はありません。(これは setWithCapacity、stringWithFormat などの他のクラスにも当てはまります)

ありがとう、サム


PSあなたの質問を助けませんが、これらの行は少し変わっています:

[self.currentTitle release];

おそらくこれを行う必要があります:

self.currentTitle = nil;

これにより、コードと同じように currentTitle が解放されますが、nil に設定されるため、誤って再度使用することはできません。

于 2010-04-19T11:16:27.943 に答える
0

私は自分の問題を理解しました。私のメモリリークはすべて、次のステートメントに起因していました。

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

これにより、ストーリーの保持カウントが 2 増加します。これは、setter 呼び出しが新しく割り当てられた配列で保持されるためです。

上記のステートメントを次のステートメントに置き換えて、問題を解決しました。

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