开发者

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

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 开发者_如何学Cline 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?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜