内存泄漏与XML解析器中的NSMutableString

时间:2012-02-01 17:29:57

标签: iphone xml-parsing instruments memory-leaks nsmutablestring

我在我的应用程序中使用XMLparser。它运行正常,但是当我在仪器中运行我的应用程序时,我不断在NSMutableString中处理内存泄漏,NSMutableString处理来自XML的字符串。

Weard事情是这个内存泄漏只在我第二次进行特定呼叫(登录)时发生。当我做其他呼叫时,我没有内存泄漏。

提供泄漏的NSMutableString是'textInProgress'。

- (NSDictionary *)objectWithData:(NSData *)data
{


    [dictionaryStack release];
    [textInProgress release];

    dictionaryStack = [[NSMutableArray alloc] init];
    textInProgress = [[NSMutableString alloc] init];

    [dictionaryStack addObject:[NSMutableDictionary dictionary]];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    BOOL success = [parser parse];
    [parser release];

    if (success)
    {

        NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
        return resultDict;
    }

    return nil;
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSMutableDictionary *parentDict = [dictionaryStack lastObject];
    NSMutableDictionary *childDict = [NSMutableDictionary dictionary];

    [childDict setObject:attributeDict forKey: NODE_ATTR_KEY];

    id existingValue = [parentDict objectForKey:elementName];
    if (existingValue)
    {
        NSMutableArray *array = nil;
        if ([existingValue isKindOfClass:[NSMutableArray class]])
        {
            array = (NSMutableArray *) existingValue;
        }
        else
        {
            array = [NSMutableArray array];
            [array addObject:existingValue];

            [parentDict setObject:array forKey:elementName];
        }

        [array addObject:childDict];
    }
    else
    {

        [parentDict setObject:childDict forKey:elementName];
    }

    [dictionaryStack addObject:childDict];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];

    if ([textInProgress length] > 0)
    {
        [dictInProgress setObject:textInProgress forKey:NODE_VALUE_KEY];

        [textInProgress release];

        textInProgress = [[NSMutableString alloc] init]; <--- Object leaks here

    }

    [dictionaryStack removeLastObject];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    [textInProgress appendString:string]; <--- Object leaks here

}

我整天都在调试,但仍无法找到解决此问题的方法。泄漏仅包含64字节的数据。

这里还有两个仪器泄漏的截图 - &gt;泄漏 - &gt;呼叫树 leak at XML parser foundCharacters

leak at XML parser didEndElement

希望有人可以帮助我!


编辑:

这些泄漏的解决方案是将NSMutableString和NSMutableDictionary声明为属性。 (请参阅Neal他的评论)

1 个答案:

答案 0 :(得分:2)

我建议您将textInProgress转换为属性而不是ivar。像这样:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];

    if ([self.textInProgress length] > 0) {

        [dictInProgress setObject:self.textInProgress forKey:NODE_VALUE_KEY];

        self.textInProgress = [NSMutableString string];
    }

    [dictionaryStack removeLastObject];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    [self.textInProgress appendString:string];
}

并确保您在dealloc

中发布

Protip:如果你也改变了属性对应的ivar的名称(例如_textInProgress),你将在之前使用过ivar的每个点都出现构建错误,所以你不会错过任何更改从textInProgressself.textInProgress