Chris Marshall
Chris Marshall

Reputation: 5332

NSXMLParser Asynch -How do I properly Release?

OK. I'm using NSXMLParser like so:

myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];

I have my own subclass in order to do things like have a memory pool and some instance data. Basically, it's OK to think of BMLT_Parser as the same as NSXMLParser.

Note that I am calling it asynchronously, so there's no simple deallocation after a synchronous call.

What I have done, is declare the following delegate function:

- (void)parserDidEndDocument:(NSXMLParser *)parser  ///< The parser in question
{
    [myParser release];
    myParser = nil;
}

myParser is an instance variable of the parser. Basically, myParser == parser in the callback.

Now, the problem is that Instruments tells me that the parser is leaking. Parsers leak badly, because they pack a lot of luggage.

How else should I dealloc asych parsers? I strongly suspect that I simply need to be directed to an "M", so that I can "RTFM".

Thanks!

Upvotes: 0

Views: 292

Answers (1)

user859045
user859045

Reputation:

myParser = [[[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]] retain];

In the above code, u r firstly allocating memory for myParser by alloc, and again u r retaining .Here, u r doing wrong as u should retain only when u have take ownership of an object.But through alloc u will get ownership of the object"myParser". And when u have used the object, u need to release that. you should do like something this..

myParser = [[BMLT_Parser alloc] initWithContentsOfURL:[NSURL URLWithString:uri]];
[myParser setDelegate:self];
[myParser performSelectorInBackground:@selector(parse) withObject:nil];
[myParser release];

Again, in the delegate definition, you r firstly releasing the object then setting that to nil.This is quite meaningless ,as if u don't have memory for any object, how can we set any value to that. write something like this..

- (void)parserDidEndDocument:(NSXMLParser *)parser  ///< The parser in question
    {
    if(_myParser)
        {
          [_myParser release];
        }
    }

Upvotes: 2

Related Questions