NSXMLParser Leaking

后端 未结 4 951
时光说笑
时光说笑 2020-11-29 06:35

I have the following code that leaks. Instruments says that it is the rssParser object that is leaking. I \"refresh\" the XML feed and it runs the block and it leaks....

相关标签:
4条回答
  • 2020-11-29 06:36

    Apple have got back to me and this is a bug #6469143

    Looks like they plan to fix for 4.0

    0 讨论(0)
  • 2020-11-29 06:38

    I just fixed this by using the method outlined in this post.

    It's a workaround, but it works.

    On another note, I have found that Instruments works reliably in Lion/Xcode 4.1 if you always run it on the device, as opposed to the simulator. On the simulator, it seems to have a devil of a time attaching to the process.

    The NSXMLParser implementation seems to be naturally leaking. There's another leak coming from this library elsewhere in my app that I need to see if I can pin down. That is an asynch call, and this solution doesn't seem to work for that.

    0 讨论(0)
  • 2020-11-29 06:53

    The most likely cause is that one of your delegate methods retains the parser. Do you do anything with your parser parameter in the delegate methods?

    Do you get a leak every time you refresh?

    If this is the only place that rssParser is used, why are you making it an ivar? If you do need an ivar, I cannot stress enough how important it is to always use accessors for them and never access them directly. The single best way to avoid memory leaks is to use accessors for your ivars.

    Also, never release something without immediately setting it to something else (usually nil). Your release of rssParser above is a crash waiting to happen because you now have a pointer to potentially unallocated memory.

    0 讨论(0)
  • 2020-11-29 06:59

    Seems this is a well know problem. See here NSURLConnection leaking. However if you set the following before initializing the parser leaking stops:

    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:URL];
    
    0 讨论(0)
提交回复
热议问题