NSXMLParser Leaking
Asked Answered
D

4

9

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

Diffusive answered 21/10, 2009 at 5:46 Comment(1)
Note that the three setShould* statements all default to NO already so you can drop those from your code.Azrael
D
10

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

Looks like they plan to fix for 4.0

Diffusive answered 22/1, 2010 at 6:44 Comment(5)
Have you had any reply from Apple about this Bug?Mcloughlin
I am getting the same leak did this leak fixed in 4.0Crosspatch
Well I still see it in the iOS4 SDK. I haven't yet downloaded the latestKa
@Phil I downloaded SDK 4.0.1 and I am still having an issue. But, I could be dumb.Precaution
Yeah, still hitting this leak all over (I'm using XML parser a couple times in my project, and I get leaks like crazy from time to time).Leonardaleonardi
H
3

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.

Hamforrd answered 21/10, 2009 at 7:0 Comment(5)
Yes I do get a leak every time, I have made the changes you describe as yes it shouldn't be an ivar! Still leaky!Diffusive
Do you have Xcode 3.2 (from SnowLeopard)? The Build and Analyze tool is very good at finding simple leaks.Hamforrd
Poking around the boards, this may be a caching issue with either NSURLConnection or NSXMLParser. You may want to open a radar on this. Google "nsxmlparser leak". iphonedevsdk.com/forum/iphone-sdk-development/… #556123Hamforrd
Thanks Rob. When you say open a radar. Is that a bug report with Apple?Diffusive
Correct. bugreport.apple.com From the stories I've heard from Apple folks, they named the original internal server that housed this stuff "radar" because by keeping track of all the bugs, it was supposed to give people almost psychic knowledge of new problems, like Radar in MASH. But this may just be Cupertino legend. CocoaDev claims it just means "it's on Apple's radar," but provide even less authority than my flimsy "from Apple folks." But I still prefer the first story. In any case, internal links to BugReporter use the URL scheme rdar://, and are widely known as "radars."Hamforrd
P
0

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];
Patiencepatient answered 9/11, 2009 at 17:50 Comment(1)
Actually Apple got back to me and this issue is already logged as 6469143. Not sure when they will fix it though. Still leaks changing to your way of doing it too!Diffusive
I
0

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.

Impresario answered 19/9, 2011 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.