提前致谢! 解决方案 如果缺少结束标记,则会出现内存泄漏.最好在 parserDidStartDocument: 中进行任何分配,并在 parserDidEndDocument: 中进行释放,因为它们保证配对.而不是在 didStartElement 中分配 resultString,你只需在那里截断它.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! 解决方案 If an end tag is missing, you will have a memory leak. It is better to have any allocations in parserDidStartDocument: and deallocations in parserDidEndDocument:, as these are guaranteed to be paired. And instead of allocating resultString in didStartElement, you just truncate it there. 这篇关于内存泄漏与 NSMutableString appendString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!