dizzy_fingers
dizzy_fingers

Reputation: 93

NSXMLParser 's delegate and memory leak

I am using a NSXMLParser class in my program and I assign a delegate to it. This delegate, though, gets retained by the setDelegate: method resulting to a minor, yet annoying :-), memory leak.

I cannot release the delegate class after the setDelegate: because the program will crash.

Here is my code:

self.parserDelegate = [[ParserDelegate alloc] init]; //retainCount:1
self.xmlParser = [[NSXMLParser alloc] initWithData:self.xmlData];
[self.xmlParser setDelegate:self.parserDelegate]; //retainCount:2
[self.xmlParser parse];
[self.xmlParser release];

ParserDelegate is the delegate class.

Of course if I set 'self' as the delegate, I will have no problem but I would like to know if there is a way to use a different class as delegate with no leaks.

Thank you in advance.

Upvotes: 0

Views: 714

Answers (2)

Paxi
Paxi

Reputation: 1

If you have the property parserDelegate set as retain and using the synthesized methods (by using self) to set it initially, then that is probably your leak.

Upvotes: 0

bobDevil
bobDevil

Reputation: 29468

From the documentation on setDelegate:

"An object that is the new delegate. It is not retained. The delegate must conform to the NSXMLParserDelegate Protocol protocol."

So no worries. Additionally, even if it was retained, a proper NSXMLParser class would release the delegate in 'dealloc'. So I don't think you have a leak.

The reason you're crashing, is you are deallocating the delegate, since you bring the retain count down to 0.

Upvotes: 2

Related Questions