Memory Leak with NSMutableString in XML parser

723 Views Asked by At

I'm using an XMLparser within my App. It's running fine however when I run my App in instruments I keep getting memory leaks at a NSMutableString which handles the strings which came out of the XML.

Weard thing is that this memory leak only occurs when I do a specific call (login) for the second time. When I do other calls I don't get a memory leak.

The NSMutableString which gives the leaks is '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

}

I've been debuggin all day and still can't find a solution for this problem. The leak only contains 64 bytes of data.

Here are also two screenshots of the leak within instruments -> Leaks -> Call tree leak at XML parser foundCharacters

leak at XML parser didEndElement

Hope someone can help me out!


EDIT:

The solution for these leaks was to declare the NSMutableString and NSMutableDictionary as a property. (Refer to Neal his comment)

1

There are 1 best solutions below

4
On BEST ANSWER

I would advise that you convert textInProgress to a property rather than an ivar. Like this:

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

And just make sure that you release it in dealloc

Protip: If you change the name of the property's corresponding ivar as well (e.g. _textInProgress), you will get a build error at each point where you used the ivar before, so you won't miss any changes from textInProgress to self.textInProgress