Skip to content

A leak when no internet connection and using initWithContentsOfURL #87

Description

@GoogleCodeExporter
There's a leaking CXMLDocument object shown in instruments everytime, a request 
to an XML is made to  a webservice AND when there's no internet connection 
available. Here's my code:

    NSString *path = ... some URL
    NSURL *url = [NSURL URLWithString: path];
    CXMLDocument *itemListParser; = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
    ... other stuff ...

If we digg deeper and trace initWithContentsOfURL call then we will find this 
method in "CXMLDocument.m":

    - (id)initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError
      {
      if (outError)
        *outError = NULL;

      NSData *theData = [NSData dataWithContentsOfURL:inURL options:NSUncachedRead error:outError];
      if (theData)
        {
        self = [self initWithData:theData encoding:encoding options:inOptions error:outError];
        }
      else
        {
            [self release]; //My suggested fix: We need to release an alloc'ed object because after the "self = null" it will be unable to release it. See the info below.
            self = NULL;
        }

      return(self);
    }

It appears, if theData is nil (for example no connection) then self will be 
nil, and so the result of the call to TouchXML initWithContentsOfURL will be 
nil too. So, in my code:

    CXMLDocument *itemListParser; = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];

I'm alloc'ing a memory but init returns nil, and so itemListParser becomes nil 
too. So later, trying to release the parser with [itemListParser release] does 
nothing because release is send to nil.

I was able to fix a leak by adding "[self release]" before "self = NULL" (see 
the line with my comment in TouchXML initWithContentsOfURL method)

---- additional notes ---
Using latest ToucXML release, objective-c, Mac OS X Snow Leopard.

Original issue reported on code.google.com by gyte...@gmail.com on 8 Dec 2011 at 12:11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions