I am using an XMLParser to parse some XML data, which uses an NSMutableString *resultString to store the tag characters. At every (- parser: didStarElement...) method I allocate and init the resultString-ivar.
- (void)parser: (NSXMLParser *)parser didStartElement: (NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName: (NSString *)qName attributes: (NSDictionary *)attributeDict {
// Alot of if-statements to sort subtags
// /.../
resultString = [[NSMutableString alloc] init];
recordResults = YES;
}
The string is appended in the parser:foundCharacters-method. I read somewhere that autoreleased objects, like the string inside appendString could cause the image of a memory leak. So i added a local autorelease pool to make sure it got drained right away (no change in behavior though):
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(recordResults) {
[resultString appendString: string];
}
[pool drain];
}
In the parser:didEndElement... I finally release and nil out the resultsString:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// Alot of if statements to handle differnt tags
// each of which has the structure of the last else-statement
// In other words, I am pretty sure I've covered every possible
// case to prevent the resultString from
// not getting released and niled out
if(...) {
...
}
else if(...) {
...
}
else {
if(resultString != nil) {
[dataDict setObject: resultString forKey: elementName];
[resultString release];
resultString = nil;
}
}
Instruments Leak-tool flags the parser:foundCharacter-method as a source for memory leakage, so I wonder if this is caused by appendString. Or if you can find something in this code that is way out wrong. This is a rather memory craving application, parsing quite a few and sometimes moderately big XML-files on an iPhone, so my question would be how to find a work around, if the NSMutableString appendString is not appropriate in this case...
Thanks in advance!