我正在寫一個應用程序,它使用NSXMLParser來解析來自網站的信息,然後將其存儲在我的應用程序中。我發佈了我在@finally塊中創建的解析器,但Instruments仍將其顯示爲內存泄漏。除此之外,應用程序的行爲非常完美。這裏的代碼:內存泄漏與NSXMLParser
NSXMLParser *myParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; //<--memory leak
@try {
if (myParser != nil)
{
[myParser setDelegate:self];
[myParser setShouldProcessNamespaces:NO];
[myParser setShouldReportNamespacePrefixes:NO];
[myParser setShouldResolveExternalEntities:NO];
}
[myParser parse];
}
@catch (NSException * e) {
NSLog(@"%@, %@",[e name], [e reason]);
}
@finally {
[xmlURL release];
[myParser release];
}
任何建議爲什麼這顯示爲內存泄漏?
**編輯* * *
下面是委託方法我已經在我的代碼實現:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI
(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"Cube"])
{
if ([attributeDict objectForKey:@"time"])
{
NSArray *pathsArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathsArray objectAtIndex:0];
NSString *timePlistPath = [NSString stringWithString:
[path stringByAppendingString:@"/time.plist"]];
NSString *TimeString = [attributeDict objectForKey:@"time"];
[TimeString writeToFile:timePlistPath atomically:YES
encoding:NSUnicodeStringEncoding error:nil];
}
if ([attributeDict objectForKey:@"currency"] && [attributeDict objectForKey:@"rate"])
{
NSString *currencyObject = [attributeDict objectForKey:@"currency"];
NSString *rateObject = [attributeDict objectForKey:@"rate"];
[mAryCurrency addObject:currencyObject];
[mAryRates addObject:rateObject];
}
}
}
難道這是原因的泄漏?
一切都在你的貼碼是否正確。這可能是NSXMLParserDelegate回調中的一些東西。嘗試在這些方法中評論代碼並查看泄漏是否仍然存在。 – Max 2011-02-28 01:23:42