raj
raj

Reputation: 59

iPhone: Memory leak in this code?

I am using two objects i.e., pagesUrl and pagesXmlParser in the code and after their use, im releasing those two objects. Its running well but its showing them in memory leaks. when i tried to verify it, i checked the reference count of those objects and its showing '1' even after the objects are released. can any one help me out how to remove that leak and how to release those particular objects in the following code.

-(void)loadPagesForChapter:(NSString *)path{
// have to parse the pages xml for this chapter

  NSURL *pagesUrl = [[NSURL alloc] initFileURLWithPath:[self pagesXmlPath:path]];

  NSXMLParser *pagesXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:pagesUrl];

  PagesAccumulator *pageLoader = [[PagesAccumulator alloc] init];
  pagesXmlParser.delegate = pageLoader;

  [pagesXmlParser parse];

// parsing pages done
// get the pages array
self.arrayOfPages = pageLoader.arrayOfPages;

    [pageLoader release];
[pagesXmlParser release];
[pagesUrl release];
NSLog(@"pagesurl retain count is:%d",[pagesUrl retainCount]);
    NSLog(@"pagesxmlparser retain count is:%d",[pagesXmlParser retainCount]);
}

Thanks in advance for any assistance.

Upvotes: 0

Views: 102

Answers (1)

deanWombourne
deanWombourne

Reputation: 38485

Ignore the retain counts

Just because an object has a retain count > 0 doesn't mean that it's not also autoreleased :)

Your code looks fine to me.

If it's really a leak I would look at your implementation of PageLoader to see if there is a retain in one of the NSXMLParserDelegate methods that's preventing the parser being released (which would cause the URL to leak as well).

Upvotes: 4

Related Questions