NSXMLParser Leaking

只愿长相守 提交于 2019-11-26 09:40:20

问题


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....

file.h

@interface TestAppDelegate : NSObject <UIApplicationDelegate> {

    NSXMLParser *rssParser;

}

file.m

NSData *data = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
    rssParser = [[NSXMLParser alloc] initWithData:data];
    [rssParser setDelegate:self];
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
    [rssParser parse];
    [rssParser release];

Image of leak....

alt text http://www.shipfinder.co.uk/images/memoryleak.png


回答1:


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

Looks like they plan to fix for 4.0




回答2:


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.




回答3:


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];



回答4:


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.



来源:https://stackoverflow.com/questions/1598928/nsxmlparser-leaking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!