NSXMLParser内存泄露
时间:2011-03-15 来源:梦之奈落
http://www.cocoachina.com/bbs/simple/?t29812.html
大家帮忙看看这里哪里有泄露
- (void)viewDidLoad {
NSMutableArray *myrssList = [[NSMutableArray alloc] initWithCapacity:1];
self.rssList = myrssList;
[myrssList release];
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *xmlFile = [paths stringByAppendingPathComponent:@"rssfeeds.xml"];
NSURL *xmlURL = [NSURL fileURLWithPath:xmlFile isDirectory:NO];
NSXMLParser *toutiaoParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[toutiaoParser setDelegate:self];
[toutiaoParser parse];
[super viewDidLoad];
}
instruments老是显示NSXMLParser *toutiaoParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];这一行有泄露,怎么回事啊。
在下面我已经清理了解析器,所以如果在这里release掉toutiaoParser,会报双释放的错误
大家帮忙看看哪里有问题
答:
随便试了个方法居然没有泄露了
我用了一个NSData的变量做了个过渡
- (void)viewDidLoad {
NSMutableArray *myrssList = [[NSMutableArray alloc] initWithCapacity:1];
self.rssList = myrssList;
[myrssList release];
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *xmlFile = [paths stringByAppendingPathComponent:@"rssfeeds.xml"];
NSURL *xmlURL = [NSURL fileURLWithPath:xmlFile isDirectory:NO];
// NSXMLParser *toutiaoParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; 这个是先前有问题的行
NSData *xmlData = [NSData dataWithContentsOfURL:xmlURL]; //用一个NSData变量过渡
NSXMLParser *toutiaoParser = [[NSXMLParser alloc] initWithData:xmlData]; //initWithContentsOfURL改成initWithData
[toutiaoParser setDelegate:self];
[toutiaoParser parse];
[super viewDidLoad];
}
下面的解析器清理
//清理解析器
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[parser release];
}
我想应该是initWithContentsOfURL有什么问题,还是学术才浅啊,都不知道这里有什么不对的.....