I have a method in my app to fetch a rss feed, and instruments shows that i’ve got a memory leak in my fetch method.
NSData* xmlData = [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString: kRSSUrl] ];
NSError *error;
GDataXMLDocument* doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if (doc != nil) {
self.loaded = YES;
NSArray* items = [[doc rootElement] nodesForXPath:@"channel/item" error:&error];
NSMutableArray* rssItems = [NSMutableArray arrayWithCapacity:[items count] ];
for (GDataXMLElement* xmlItem in items) {
[rssItems addObject: [self getItemFromXmlElement:xmlItem] ];
}
[self.delegate performSelectorOnMainThread:@selector(updatedFeedWithRSS:) withObject:rssItems waitUntilDone:YES];
} else {
[self.delegate performSelectorOnMainThread:@selector(failedFeedUpdateWithError:) withObject:error waitUntilDone:YES];
}
[doc autorelease];
[xmlData release];
Instruments throw this:
Leaked Object # Address Size Responsible Library Responsible Frame
Malloc 16 Bytes,4 < multiple > 64 Bytes appname -[RSSLoader fetchRss]
EDIT
My getItemFromXmlElement method:
-(NSDictionary*)getItemFromXmlElement:(GDataXMLElement*)xmlItem
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[[[xmlItem elementsForName:@"title"] objectAtIndex:0] stringValue], @"title",
[[[xmlItem elementsForName:@"link"] objectAtIndex:0] stringValue], @"link",
[[[xmlItem elementsForName:@"description"] objectAtIndex:0] stringValue], @"description",
nil];
}
Instruments is not telling you that this routine leaked, only that one of the objects created in this routine was leaked.
My guess is the
rssItemsarray passed toupdatedFeedWithRSSis over-retained at some point, by eitherupdatedFeedWithRSSor something it calls. Us verifying this would take posting a lot of code, and it’s not worth it. Just read through and see if you can find it.