开发者

iPhone: Memory leak in this code?

开发者 https://www.devze.com 2023-01-30 17:39 出处:网络
I am using two objects i.e., pagesUrl and pagesXmlParser in the cod开发者_开发百科e and after their use, im releasing those two objects. Its running well but its showing them in memory leaks. when i t

I am using two objects i.e., pagesUrl and pagesXmlParser in the cod开发者_开发百科e 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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消