开发者

Objective C, libxml2 memory leak when parsing xml

开发者 https://www.devze.com 2023-02-09 11:22 出处:网络
I am relatively new to objective C and coming from Java the whole memory management didn\'t completely got to me.

I am relatively new to objective C and coming from Java the whole memory management didn't completely got to me.

I am using libxml2 to parse xml data and when I run my program with performance tools I get memory leaks and it says Responsible Library is libxml2.2.dylib.

Below the code where I use the xml library. Does anyone see obvious mistakes I do? Let me know if you need more details.

Cheers, Stephan

- (void)fetchedXML:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)retrievedData error:(NSError *)error{
if (retrievedData) {
    if(error==nil){
        NSString *xpathQueryString = nil; 
        NSArray *nodes = nil;

        //get total amount of persons
        xpathQueryString = @"//data/Person"; 
        nodes = PerformXMLXPathQuery(retrievedData, xpathQueryString);  

        int personCount = [nodes count];

        for (int i=1; i<=personCount; i++) {
            NSString *position = [NSString stringWithFormat:@"%d", i];                  
            NSArray *matchNodes = nil;

            xpathQueryString = [NSString stringWithFormat:@"//data/Person[%@]/FirstName/text()", position]; 
            matchNodes = Perform开发者_JS百科XMLXPathQuery(retrievedData, xpathQueryString);  
            NSString *personFirstName = [XMLHelper fetchContent:matchNodes];

            xpathQueryString = [NSString stringWithFormat:@"//data/Person[%@]/LastName/text()", position];  
            matchNodes = PerformXMLXPathQuery(retrievedData, xpathQueryString);  
            NSString *personLastName = [XMLHelper fetchContent:matchNodes];

            xpathQueryString = [NSString stringWithFormat:@"//data/Person[%@]/Job/text()", position];  
            matchNodes = PerformXMLXPathQuery(retrievedData, xpathQueryString);  
            NSString *personJob = [XMLHelper fetchContent:matchNodes];

            [Person storePerson:managedObjectContext personId:position firstName:personFirstName lastName:personLastName job:personJob];
        }
    }
  }         
}

That's the code in the LinkHelper class.

+ (NSString *)fetchContent:(NSArray *)nodes { 
NSString *result = nil; 
for ( NSDictionary *node in nodes ) {
    for ( id key in node ) { 
        if( [key isEqualToString:@"nodeContent"] ) {
            result = [node objectForKey:key];
        }
    } 
}
return result;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消