开发者

NSXMLParser refresh leak

开发者 https://www.devze.com 2023-02-17 19:50 出处:网络
I have strange problem with my app. When I open my app it parses an RSS feed and displays the results in a tableview and there are no leaks BUT if I press my refresh button it leaks (a lot) :(

I have strange problem with my app.

When I open my app it parses an RSS feed and displays the results in a tableview and there are no leaks BUT if I press my refresh button it leaks (a lot) :(

My Project: Link

Code:

viewDidLoad

UIBarButtonItem *ShareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(tableView_ReFresh)];
self.navigationItem.rightBarButtonItem = ShareButton;
[ShareButton release];

viewDidAppear

if ([stories count] == 0) {
    NSURL *path = [[NSURL alloc] initWithString:@"http://feeds.feedburner.com/TheAppleBlog"];
    [self parseXMLFileAtURL:path];
    [path release];
}

tableView_ReFresh

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *path = [[NSURL alloc] initWithString:@"http://feeds.feedburner.com/TheAppleBlog"];
[self parseXMLFileAtURL:path];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[path release];
[pool drain];

reloadData

[blogTable reloadData];

parseXMLFileAtURL

stories = [[NSMutableArray alloc]init];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSData *xml = [NSData dataWithContentsOfURL:URL];   
rssParser = [[NSXMLParser alloc] initWithData:xml];
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:开发者_StackOverflow中文版NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser setDelegate:self];
[self.view setNeedsDisplay];
[rssParser parse];
[xml release];

didStartElement

currentElement = [[elementName copy]autorelease];
if ([elementName isEqualToString:@"item"]) {
    // clear out our story item caches...
    item = [[[NSMutableDictionary alloc] init]autorelease];
    currentTitle = [[NSMutableString alloc] init];
    currentDate = [[NSMutableString alloc] init];
    currentContent = [[NSMutableString alloc] init];
    currentPostID = [[NSMutableString alloc] init];
    currentCommentLink = [[NSMutableString alloc] init];
    currentCommentNum = [[NSMutableString alloc] init];
}

didEndElement

if ([elementName isEqualToString:@"item"]) {
    // save values to an item, then store that item into the array...
    [item setObject:currentTitle forKey:@"title"];
    [item setObject:currentPostID forKey:@"link"];
    [item setObject:currentContent forKey:@"Content"];
    [item setObject:currentDate forKey:@"date"];
    [item setObject:currentCommentLink forKey:@"commentRssLink"];
    [item setObject:currentCommentNum forKey:@"commentsNum"];

    [stories addObject:[item copy]];
    NSLog(@"adding story: %@", currentPostID);
    //NSLog(@"adding story: %@", currentContent);
}

foundCharacters

if ([currentElement isEqualToString:@"title"]) {
    [currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"guid"]) {
    [currentPostID appendString:string];
} else if ([currentElement isEqualToString:@"content:encoded"]) {       
    [currentContent appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
    [currentDate appendString:string];
} else if ([currentElement isEqualToString:@"wfw:commentRss"]) {
    [currentCommentLink appendString:string];
} else if ([currentElement isEqualToString:@"slash:comments"]) {
    [currentCommentNum appendString:string];
}

parserDidEndDocument

[blogTable reloadData];

dealloc

[super dealloc];
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentTitle release];
[currentDate release];
[currentContent release];
[currentPostID release];
[currentCommentLink release];
[currentCommentNum release];

How do I fix this? Thanks


My guess this is your leak:

stories = [[NSMutableArray alloc]init];

You're releasing stories in your dealloc, which I assume is a property, which I assume you have declared with retain. If I am correct, you should be able to fix this by either doing (A) self.stores = [[NSMutableArray alloc]init]; or (B) by putting [stories release]; before that line.

Assuming your version of Xcode is reasonably up-to-date, if you run your code with Build & Analyze I would guess it would catch this issue.


I think you need to read the documentation about memory management. First of each time you parse you are allocating the NSXMLParser again and then for each "item" element in the XML file you're allocating your strings again without releasing them.

The easiest route, without fully understanding the problem, is to create properties for your ivars and call 'self.ivar =' when you want to assign a new value to them. The generated setters will take care of releasing the memory that the variable was previously pointing to.

I strongly recommend that you read this though: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号