views:

39

answers:

1

I've been trying for some time now to get xml parser working leak free and efficient but so far unsuccessful. I've removed additional fields as they are all the same.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    currentElement = [[elementName copy] autorelease];
    if ([elementName isEqualToString:@"source"]) {
        if (!currentID) {
            overlays = [[NSMutableArray alloc] init];
            currentID = [[NSMutableString alloc] init];
        } else {
            [currentID release];
            currentID = nil;
            currentID = [[NSMutableString alloc] init];
        }
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"source"]) {
         overlay = [[NSMutableDictionary alloc] init];
         [overlay setObject:currentID forKey:@"id"];
        [overlays insertObject:overlay atIndex:[overlays count]];
        [overlay release];
        overlay = nil;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
   string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
   string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
   if ([currentElement isEqualToString:@"id"]) {
        [self.currentID appendString:string];
   }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [currentID release];
    currentID = nil;
    //[self addResources];
 }

Any help would be appreciated.

+1  A: 

If you have declared properties for your ivars I would recommend to use them. This makes memory management easier. This is how I would write your code:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    self.currentElement = elementName;
    if ([elementName isEqualToString:@"source"]) {
        if (!self.currentID) {
            self.overlays = [NSMutableArray array];
            self.currentID = [NSMutableString string];
        } else {
            self.currentID = [NSMutableString string];
        }
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"source"]) {
        [self.overlays addObject:
        [NSDictionary dictionaryWithObjectsAndKeys:self.currentID,@"id",nil]];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
   string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
   string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
   if ([self.currentElement isEqualToString:@"id"]) {
        [self.currentID appendString:string];
   }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    self.currentID = nil;
}

You might also want to try Build&Analyse for your code. this may point out where you are leaking.

tonklon
If I could vote you up 10 times I would. I did try the static analyser but in my project it hasn't found any leaks where there are leaks and find leaks where there aren't. Go figure.
Rudiger