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

Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122

4 Answers4

10

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

Looks like they plan to fix for 4.0

Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 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! – Lee Armstrong Oct 21 '09 at 19:17
  • Do you have Xcode 3.2 (from SnowLeopard)? The Build and Analyze tool is very good at finding simple leaks. – Rob Napier Oct 22 '09 at 00:13
  • 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". http://www.iphonedevsdk.com/forum/iphone-sdk-development/4910-nsxmlparser-rssparser-causing-memory-leak.html http://stackoverflow.com/questions/555623/got-memory-leak-problem-when-i-used-nsxmlparser-same-as-seismicxml-example – Rob Napier Oct 22 '09 at 13:31
  • Thanks Rob. When you say open a radar. Is that a bug report with Apple? – Lee Armstrong Oct 25 '09 at 06:18
  • 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." – Rob Napier Oct 26 '09 at 01:40
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];
leviathan
  • 11,080
  • 5
  • 42
  • 40
  • 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! – Lee Armstrong Nov 11 '09 at 13:28
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.

Chris Marshall
  • 4,910
  • 8
  • 47
  • 72