am writing an application that parses some xml and creates a dictionary of the element names and their values using the below key methods:
- (void)parserDidStartDocument:(NSXMLParser *)parser{
foundCharacters = [[NSMutableString alloc] init];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (accumulator) {
[foundCharacters appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([foundCharacters length] != 0) {
[parsedContent setObject:[foundCharacters copy] forKey:elementName];
}
}
The leak occurs in the occurs on the “[parsedContent setObject:[foundCharacters copy] forKey:elementName];” line, I can’t work out a way to overcome this.
Any insights would be much appreciated.
Edit:
The memory leak only occurs if the request to parse is called more than once
I’ve also tried “[[foundCharacters copy] autorelease]” but no to avale
In your
parserDidStartDocument:you alloc/init a mutable string, but I don’t see code where you dispose of this string again. You need to release the string again, for example in theparserDidEndDocument:method.The memory leak is reported later probably because this is the last place where the variable was actually referenced.