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
Apple have got back to me and this is a bug #6469143
Looks like they plan to fix for 4.0
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.
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];
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.