内存泄漏与 NSMutableString appendString [英] Memory Leak with NSMutableString appendString
问题描述
我正在使用 XMLParser 来解析一些 XML 数据,它使用 NSMutableString *resultString 来存储标记字符.在每个 (- parser: didStarElement...) 方法中,我分配并初始化 resultString-ivar.
- (void)parser: (NSXMLParser *)parser didStartElement: (NSString *)elementName namespaceURI: (NSString *)namespaceURIqualifiedName: (NSString *)qName 属性:(NSDictionary *)attributeDict {//大量的 if 语句来对子标签进行排序///.../resultString = [[NSMutableString alloc] init];记录结果 = 是;}
字符串附加在解析器中:foundCharacters-method.我在某处读到自动释放的对象,例如 appendString 中的字符串可能导致内存泄漏的图像.所以我添加了一个本地自动释放池以确保它立即被耗尽(尽管行为没有变化):
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];如果(记录结果){[resultString appendString: 字符串];}[池排水];}
在解析器中:didEndElement...我终于释放并清除resultsString:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName {//很多 if 语句来处理不同的标签//每个都有最后一个 else 语句的结构//换句话说,我很确定我已经涵盖了所有可能的//防止 resultString 的情况//没有被释放和清除如果(...) {...}否则如果(...){...}别的 {if(resultString != nil) {[dataDict setObject: resultString forKey: elementName];[resultString 发布];结果字符串 = 零;}}
Instruments Leak-tool 将 parser:foundCharacter-method 标记为内存泄漏的来源,所以我想知道这是不是由 appendString 引起的.或者如果你能在这段代码中找到一些错误的东西.这是一个相当需要内存的应用程序,在 iPhone 上解析相当多的、有时是中等大小的 XML 文件,所以我的问题是如何找到解决方法,如果 NSMutableString appendString 在这种情况下不合适......>
提前致谢!
如果缺少结束标记,则会出现内存泄漏.最好在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!