Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSXMLParser Leaking

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

like image 487
Lee Armstrong Avatar asked Oct 21 '09 05:10

Lee Armstrong


Video Answer


2 Answers

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

Looks like they plan to fix for 4.0

like image 169
Lee Armstrong Avatar answered Oct 02 '22 15:10

Lee Armstrong


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.

like image 23
Rob Napier Avatar answered Oct 02 '22 17:10

Rob Napier