I am using TouchXML to parse XML. While i am running the same in “Profile” Mode. I can able to see the memory leak. How can I fix this issue?
-(NSMutableArray *) grabXML:(NSData *)xmlData andQuery:(NSString *)query {
NSMutableArray *blogEntries = [[[NSMutableArray alloc] init] autorelease];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithData:xmlData options:0 error:nil] autorelease];
NSArray *resultNodes = [rssParser nodesForXPath:query error:nil];
for (CXMLElement *resultElement in resultNodes) {
NSMutableDictionary *blogItem = [[NSMutableDictionary alloc] init];
int counter;
for(counter = 0; counter < [resultElement childCount]; counter++) {
[blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
}
for( int i = 0; i < [[resultElement attributes] count]; i++) {
CXMLNode *node = [[resultElement attributes] objectAtIndex:i];
[blogItem setObject:[[resultElement attributeForName:[node name]] stringValue] forKey:[node name]];
}
[blogEntries addObject:[blogItem copy]];
[blogItem release];
}
return [blogEntries copy];
}
I am calling the above method in the below mentioned format.
NSMutableArray *arr = [[self grabXML:responseData andQuery:@"//wsCheneliereResult"] autorelease];
Here is your mistake : you do not need to copy the
blogItem,addObject:will actuallyretainits argument so that you can safely call[blogItem release];afterward.And you most probably should not do
return [blogEntries copy]either, and return justblogEntries: this is common programming habit that methods whose name does not contain ‘copy’ or ‘create’ return autoreleased values, such as you do not need to release them when you are done with them.To sum up, I would go with :
and