2011-03-20 81 views
0


我的應用程序出現奇怪的問題。
當我打開我的應用程序在解析一個RSS提要並顯示在tableview中的結果,並沒有泄漏,但如果我按下刷新按鈕,它泄漏(很多):(NSXMLParser刷新漏洞

我的項目:Link

代碼:
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: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]; 

我該如何解決這個問題? 謝謝

回答

0

我認爲你需要閱讀關於內存管理的文檔。每解析一次,首先分配NSXMLParser,然後爲XML文件中的每個「item」元素分配字符串而不釋放它們。

最簡單的路線,如果不完全瞭解問題,就是爲您的ivars創建屬性,並在您想爲它們分配新值時調用'self.ivar ='。生成的setter將負責釋放變量先前指向的內存。

我強烈建議你閱讀這雖然: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

0

我的猜測,這是您的泄漏:

stories = [[NSMutableArray alloc]init]; 

你在你的dealloc,我假設釋放stories是一個屬性,該屬性我假設你已經用retain宣佈。如果我是正確的,你應該能夠通過(A)self.stores = [[NSMutableArray alloc]init];或(B)通過在該行之前放置[stories release];來解決此問題。

假設你的Xcode版本是合理的最新版本,如果你使用Build &運行你的代碼分析我猜想它會解決這個問題。

+0

它不工作:(你能看看我的項目嗎?謝謝 – user535351 2011-03-21 02:16:52

+0

@ user535351對不起,我沒有時間看你的項目。同時,你看了我給你的建議(運行Build&Analyze看看它說了什麼)?你有沒有讀過關於內存管理的Apple文檔? – 2011-03-21 03:11:34