Memory leak in XML Parser
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:(NSStri开发者_高级运维ng *)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.
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.
The other bug in your code is:
[tempString appendString:[[XMLParser alloc]
stripUnwantedStringChars:string]];
This allocates a new XMLParser
and never gets rid of it.
I'm trying to figure out what you ended up doing in your assignment to tempString. If you're doing this:
self.tempString = [NSMutableString string];
then you do have to release tempString in dealloc. Even though it's being autoreleased, the setter is retaining it.
First of all, I would imagine this should actually crash, since the autoreleased mutable string should be released shortly after you return from parserDidStartDocument. That it doesn't is kind of concerning, and you are also lying in your property definition by claiming the property is retained when it has not been.
However, what Leaks is telling you is that that the copy of the string has been leaked - Leaks shows you where the object leaking was allocated, but that's not the cause of the leak. The cause of the leak is the line of code you don't have that should be releasing that string properly later. Since Leaks can't point to code that does not exist, all it can show you is what made the object that is not being freed correctly.
In this case, I think what you are missing is that an array should hold objects that are autoreleased - so you want to say:
[entryDict setObject:[[tempString copy] autorelease] forKey:kXMLDictDateKey];
Because Copy also retains the copy (just like alloc/init would).
精彩评论