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.
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.