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
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)
I would advise that you convert
textInProgress
to a property rather than an ivar. Like this: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 fromtextInProgress
toself.textInProgress