Can't find the leak in this Objective-C NSXMLParser code?

寵の児 提交于 2019-12-25 03:27:21

问题


I am new to iPhone programming, and am running my app against the Leaks tool in Instruments. The tool is finding a few leaks, all of which seem to point to 1 line of code in a class which uses NSXMLParser:

- (BOOL)parse{  
    NSURL *url = [[NSURL alloc] initWithString:@"[url here]"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    NSLog(@"NSXMLParser initialized");

    [parser parse];

    [url release];
    [parser release];
    return YES;
}

The tool points to the line creating the parser as having the leak:

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Can anyone point me in the right direction on this one? I've been going through reference counts in my code for an hour now with no luck.

UPDATE:

Ok, taking the suggestions from 2 answers, I added these lines before creating the NSURL:

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

And I added this line right before releasing the parser:

[parser setDelegate:nil];

Each addition reduced the number of leaks, and now I'm down to 2. One points to CFNetwork and one points to Foundation as the responsible library. Examining the call stack on both doesn't show any of my code at all.

Is there anything else I might be doing wrong here?


回答1:


I found on this on another thread:

[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];

(source: another stackoverflow topic)

But I haven't test this solution.




回答2:


Have you tried setting the parser's delegate to nil before releasing it? Some classes tend to retain their delegates...

EDIT:

I didn't actually see that the parser was an NSXMLParser instance. Reading the docs I can see that they state that: "It is not retained.". Still I would give it a try.




回答3:


If that really is the entirety of your code, then it really does sound like there is a leak in the underlying framework. Zip up your sample program and file a bug, please.

However, it isn't the entirety of the code -- if your delegate method is retaining anything and not balancing it with a release, it will cause a leak. The confusion may be because the Leaks instrument shows the point of allocation of the object when the actual leak may be caused by a subsequent retain.

Using the Object Alloc instrument and leaks detection, in particular, you can drill down to see exactly who is retaining and releasing any given object. That may shed some light on the situation.




回答4:


I had the same problem and I suspect that the leak is in the underlying framework.

I changed my code from:

NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileName];

to this:

NSData * fileData = [[NSData alloc] initWithContentsOfURL:fileName];
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:fileData];
[parser setDelegate:self];
[parser parse];
[parser release]; 
[fileData release];

and the leak went away.



来源:https://stackoverflow.com/questions/2298463/cant-find-the-leak-in-this-objective-c-nsxmlparser-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!