R – Memory leak in XML Parser

iphonememory-leaksmemory-managementnsxmlparserobjective c

I use NSXMLParser for parsing an XML document. I have the following functions (among others):

- (void) parserDidStartDocument:(NSXMLParser *)parser {

    // Init tempString
    tempString = [NSMutableString string];

}    
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

        // save gained data for element "date"
        if ([elementName isEqualToString:@"date"])
            [entryDict setObject:[tempString copy] forKey:kXMLDictDateKey];

        [tempString setString:@""];
    }


    //
    // Character Handling
    //
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
        [tempString appendString:[[XMLParser alloc] stripUnwantedStringChars:string]]; //Just strips tabs and linebreaks and the returns the string
    }

tempString is an instance variable with the following property:

@property (nonatomic, retain) NSMutableString *tempString;

tempString does not have to be released in dealloc as it is initiated with a convenience method, thus it is automatically assigned to an autorelease pool. I have also tried the following with an alloc, init approach, yet with the same result. So here is what I did:

1.) Run my project with instruments, let it search for leaks right after startup, there are none.
2.) run the XML parser once, check for leaks. There are none.
3.) run the XML Parser again, now suddenly the line with [entryDict setObject:[tempString copy] forKey:kXMLDictDateKey]; leaked.

I have been looking into these memory leaks for hours now, what did I forget? If you need more code please let me know, although I think my problem is somewhere in these lines.

Ps. My checks show that between the parser (delegate) calls the "dealloc" method gets called, thus I think the parser is really loaded two times, not just once.

Best Answer

Your call to:

tempString = [NSMutableString string];

Actually does not invoke the property (wrapper) and the retain.

You should do this instead:

self.tempString = [NSMutableString string];

Otherwise you are just setting the ivar directly to an autoreleased object.

Not only do you have a leak somewhere, the above code will at one point result in some interesting crash.

Related Topic