2

I am having trouble removing leaks from an iPhone app that i'm working on. I am parsing an xml feed to get data. Here is the code I am using to parse

[[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0]; 
    NSData *xml = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"url link"]];
    self.parser = [[NSXMLParser alloc] initWithData:xml];   


[self.parser setDelegate:self];
[self.parser parse];
[self.parser release];
self.parser=nil;

And the parsing code

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if(![elementName compare:@"item"])
{tempElement = [[XMLElement alloc] init];
}
else if(![elementName compare:@"title"])
{
    self.currentAttribute = [NSMutableString string];
}

else if(![elementName compare:@"link"])
{
    self.currentAttribute = [NSMutableString string];
}
else if(![elementName compare:@"comments"])
{
    self.currentAttribute = [NSMutableString string];
}
else if(![elementName compare:@"pubDate"])
{
    self.currentAttribute = [NSMutableString string];
}
else if(![elementName compare:@"category"])
{
    self.currentAttribute = [NSMutableString string];
}

else if(![elementName compare:@"description"])
{
    self.currentAttribute = [NSMutableString string];
}}

I am getting a leak on each

self.currentAttribute = [NSMutableString string];

and on

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{NSString *strAppend = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([strAppend length] > 0) {
    [self.currentAttribute appendString:string];
}

}

Any help is appreciated. Thanks in advance

Wizard Of iOS
  • 2,389
  • 2
  • 18
  • 22

2 Answers2

1

If your properties are being retained, like this:

@property (nonatomic, retain) NSXMLParser * parser;

...then they're getting retained twice. So one release won't do it. You could set them up like this:

NSXMLParser *tempParser = [[NSXMLParser alloc] initWithData:xml];
self.parser = tempParser;
[tempParser release];

Then do whatever you need with self.parser. Then in your dealloc method, you release it. Since this way it's left with a retain count of 1 (the original alloc retain having been released with tempParser), one release should do it:

- (void) dealloc {
[parser release];
[super dealloc];
}

Also, I'm puzzled why you would go to the trouble of writing if-else statements if each possibility has the same result.

Wienke
  • 3,723
  • 27
  • 40
0

Thanks a lot but, i was not calling the dealloc function inside my object class. Adding this function and releasing all strings using [self.mystring release] solved the problem

Wizard Of iOS
  • 2,389
  • 2
  • 18
  • 22