NSXMLParser内存泄露

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有什么问题,还是学术才浅啊,都不知道这里有什么不对的.....

转载于:https://www.cnblogs.com/APTX4869/archive/2011/03/15/1985066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值