views:

270

answers:

3

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  A: 

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.

Yannick L.
bbum: Did you mean "shouldn't"?
Colin Barrett
I tried this, and now Leaks does show some leaky objects, but when I drill into the call stack, it's all framework methods and nothing points to my code. Is this normal?
Mark Struzinski
Yah... shouldn't shouldn't shouldn't be counted as a leak. :)
bbum
+1  A: 

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.

bbum
Alternately, if there's more code that he's omitted, he should paste that too.
Colin Barrett
I've got the delegate methods which handle the actual parsing, but since Leaks didn't point to any of them, I didn't think they would be the culprits. Will leaks point to the calling method rather than the place where the leaky object was created?
Mark Struzinski
+1  A: 

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.

Dimitris