I have set up an NSXMLParser to parse through a Twitter feed, but I am getting memory leaks, and after sitting through using instruments about a hundred times I can’t figure out where it is! Below is my NSXMLParser code that parses when ASIHTTPRequest receives data back.
-(void)ProcessAndParse{
NSURL *url = [config urlForFeed:@"Twitter"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
myparser = [[NSXMLParser alloc] initWithData:responseData];
[myparser setDelegate:self];
[myparser setShouldResolveExternalEntities:YES];
[myparser parse];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
I know there is quite a bit missing here but I am trying to stop this leak before I go on any further. It seems to center around the currentElement = [elementName copy];in the didStartElement method.
You need to release the previous element, before assigning a new one for currentElement, otherwise the old element won’t have any other references and it will be leaked. So you can do this:
Alternatively you can declare your ivar currentElement property as copy, and simply do this: