内存泄漏的NSMutableString appendString [英] Memory Leak with NSMutableString appendString

查看:698
本文介绍了内存泄漏的NSMutableString appendString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是XMLParser的解析一些XML数据,它使用一个的NSMutableString * resultString存储标记字符。在每一个( - 解析器:didStarElement ...)方法我分配和初始化的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;
}

foundCharacters法:

该字符串在解析器追加。我读的地方,自动释放的对象,如内appendString字符串可能会导致内存泄漏的形象。所以我加了一个本地autorelease池,以确保它得到倒掉马上(不会发生任何变化虽然):

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];
}

在解析器:didEndElement ...我终于释放和无出resultsString:

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;
    }
}

仪器检漏工具标志解析器:foundCharacter法作为内存泄漏的来源,所以我不知道这是由appendString造成的。或者,如果你能找到这个code是出路错误的东西。
这是一个相当内存渴求应用,解析在iPhone上不少,有时适度的大XML的文件,所以我的问题是如何找到一个变通,如果的NSMutableString appendString是不是在这种情况下,适当的...

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...

在此先感谢!

推荐答案

如果结束标记丢失,你将有一个内存泄漏。最好是有parserDidStartDocument任何分配:在parserDidEndDocument和释放:,因为这些都是保证配对。而不是在didStartElement分配resultString,你只是截断它。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆