user1273250
user1273250

Reputation: 11

Reference-counted object is used after it is release? NSXMLParser

I've download this code from this website. http://homepages.ius.edu/rwisman/C490/html/nsxmlparser.htm

As I analyze it. I have memory leaks. I've tried google everything from autorelease, sender, nil ,[parser setDelegate: [Top10Parser new]], etc. it doesn't work.

I NEED HELP.

- (void) run: (id) param  {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

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

[parser setDelegate: self]; <- ("Reference-counted object is used after it is release")

    [parser parse];
    [parser release];

    if ([delegate respondsToSelector:@selector(parseDidComplete:)])
        [delegate performSelector:@selector(parseDidComplete:) onThread: thread 
                       withObject: outstring waitUntilDone:NO];

    [pool release];
} 

Upvotes: 0

Views: 137

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90571

Change these two lines:

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

to

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

To some schools of thought, +alloc returns an owned object and init methods neither add nor remove ownership. It is more correct -- and it is how the analyzer treats it -- to think of an init method consuming a reference on the receiver and returning an owned object. So, if you call an init method but don't assign the result to self, then the ownership you had for self has been consumed and you have no pointer to the new owned object. All future uses of self are using an object you no longer own.

Upvotes: 2

Related Questions