2010-11-14 65 views
0

我在iOS 4上開發,並且我的appDelegate中有這個方法。它由幾個tableviews數據源代表調用。儀器給我這些(在設備和模擬器上) Malloc 512字節,Malloc 512字節,NSConcreteMapTable(基金會)。 Mallocs沒有顯示任何負責任的圖書館。當方法返回對象時iPhone內存泄漏

這裏的返回對象的方法:

- (NSXMLParser *) getXmlParserFrom:(NSString *)remoteFile andCacheToFile:(NSString *) fileName forceRefresh:(BOOL) doRefresh { 
    NSXMLParser *xmlParser; 

    //FIRST TRY TO LOAD THE XML FROM CACHED FILE 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    NSString *xmlDocumentFromCache = [[NSString alloc] initWithContentsOfFile:filePath]; 

    if (xmlDocumentFromCache && !doRefresh) { 
     NSData *xmlData = [NSData dataWithContentsOfFile:filePath]; 
     xmlParser = [[NSXMLParser alloc] initWithData:xmlData]; 
    } else { 
     NSURL *xmlFileURL = [NSURL URLWithString:remoteFile]; 
     NSString *contentsOfRemoteFile = [NSString stringWithContentsOfURL:xmlFileURL]; 

     //CACHE THE FILE 
     BOOL cacheResult = [contentsOfRemoteFile writeToFile:filePath atomically:YES]; 
     xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFileURL]; 
    } 
    [xmlDocumentFromCache release]; 

    return [xmlParser autorelease]; 
} 
+0

Dup of [NSXMLParser Leaking](http://stackoverflow.com/questions/1598928/nsxmlparser-leaking)。基本上,這是NSXMLParser中的一個錯誤(#6469143)。應該在即將推出的iOS版本中修復。檢查解決方法的鏈接問題的答案。另請閱讀iPhon Dev上的[「NSXMLParser * rssParser導致內存泄漏」](http://www.iphonedevsdk.com/forum/iphone-sdk-development/4910-nsxmlparser-rssparser-causing-memory-leak.html) SDK。使用谷歌搜索「NSConcreteMapTable NSXMLParser」可以找出bug上的其他頁面。 – outis 2010-11-14 10:59:17

回答

0

我只是通過使用this post概述的方法解決了這個問題。

這是一個解決方法,但它的工作原理。

另一方面,我發現儀器在Lion/Xcode 4.1中可靠地工作,如果你總是在設備上運行它,而不是模擬器。在模擬器上,似乎有一個附加在過程上的魔鬼。