代码之家  ›  专栏  ›  技术社区  ›  Rudiger

XMLParser泄漏问题

  •  0
  • Rudiger  · 技术社区  · 14 年前

    我已经尝试了一段时间让xml解析器无泄漏、高效地工作,但到目前为止还没有成功。我删除了其他字段,因为它们都是相同的。

    - (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];
     }
    

    任何帮助都将不胜感激。

    1 回复  |  直到 14 年前
        1
  •  1
  •   tonklon    14 年前

    - (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;
    }
    

    您还可以尝试构建;分析你的代码。这可能会指出你泄漏的地方。