A work around to the memory leaks in NSXMLParser

Posted by Filip Ekberg on November 30 2010 7 Comments

Lately I’ve experimenting a bit with parsing XML in Objective-C and discovered something that I’d like to share with you all.

First of all, consider the following “standard” way of downloading an XML and parsing it

NSXMLParser *parser = [[NSXMLParser alloc]
            initWithContentsOfURL: [NSURL
            URLWithString:@"http://some-xml-url.com/my.xml"]];

You would think that this line of code is Solid, all it’s supposed to do is download the content, release the temporary resources for fetching data and store the final content for the parsing later on. However somewhere in the process there’s a resource that isn’t cleaned up properly and you will get a memory warning the “Build and Analyze” method wont find it but when you run the application with a Performance tool looking for leaks you will end up getting something like this:

Leaked Object
Malloc 512 Bytes

Responsible Frame
allocateCollectableUnscannedStorage

Leaked Object
NSConcreteMapTable

Responsible Frame
+[NSMapTable alloc]

I tried a lot of suggestions to fix this issue some of them being to set the sharedURLCache to 0 like this:

[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];

These alone did not solve the problem, so on the quest to find a proper work around I came up with this solution:

[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];

NSData *xml = [NSData
        dataWithContentsOfURL: [NSURL
        URLWithString:@"http://some-xml-url.com/my.xml"]];

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xml];;

And that did it, after this I no longer received any memory warnings.

Vote on HN

7 Responses to A work around to the memory leaks in NSXMLParser

  1. kimNo Gravatar says:

    Thank you.. my problem is solved.. :grin:

  2. MikeNo Gravatar says:

    Wow that actually makes a difference, initing it with contents of URL or data! :smile:

  3. iphonesekNo Gravatar says:

    Tnx it Works!!!

  4. BartNo Gravatar says:

    Thanks that really works!

    One clue it also works without:
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];

    It might be that problem is inside initializing NSXMLParser with URL.

  5. BillNo Gravatar says:

    Filip,

    Thanks for posting this. Not sure how I would have been able to fix these leaks other wise. Your my new best friend!

    Bill

  6. AndersNo Gravatar says:

    Thanks for this, you just saved my day :)

  7. joepoeNo Gravatar says:

    TACK! Två veckor förlorade jag på att försöka täta denna läckan. Sedan hittade jag din post.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>