topfunky / hpple

An XML/HTML parser for Objective-C, inspired by Hpricot.
http://topfunky.com
MIT License
2.77k stars 471 forks source link

Memory leak FIX in : NSDictionary *DictionaryForNode #69

Closed RachidAbouSalman closed 7 years ago

RachidAbouSalman commented 9 years ago

Hi,

thx for this library, I was using it and I saw a memory leak while using it. I had a quick look at it, please test the fix below (lines with "//RNR")

hope it helps, it corrected my issue :-)

NSDictionary DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary parentResult,BOOL parentContent) { NSMutableDictionary resultForNode = [NSMutableDictionary dictionary]; if (currentNode->name) { NSString currentNodeContent = [NSString stringWithCString:(const char *)currentNode->name encoding:NSUTF8StringEncoding]; resultForNode[@"nodeName"] = currentNodeContent; }

xmlChar *nodeContent = xmlNodeGetContent(currentNode);
if (nodeContent != NULL) {
    NSString *currentNodeContent = [NSString stringWithCString:(const char *)nodeContent
                                                      encoding:NSUTF8StringEncoding];
    if ([resultForNode[@"nodeName"] isEqual:@"text"] && parentResult) {
        if (parentContent) {
            NSCharacterSet *charactersToTrim = [NSCharacterSet whitespaceAndNewlineCharacterSet];
            parentResult[@"nodeContent"] = [currentNodeContent stringByTrimmingCharactersInSet:charactersToTrim];
            xmlFree(nodeContent); //RNR
            return nil;
        }
        if (currentNodeContent != nil) {
            resultForNode[@"nodeContent"] = currentNodeContent;
        }
        xmlFree(nodeContent); //RNR
        return resultForNode;
    } else {
        resultForNode[@"nodeContent"] = currentNodeContent;
    }
    xmlFree(nodeContent);
}

Rachid

sydowma commented 7 years ago

Thank you!!!!!!!!!!!!!!!!! This problem has been bothering me for a long time, thank you very much for your

topfunky commented 7 years ago

This has been fixed in pull request #80