首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libxml2.2.dylib创建大量内存泄漏

libxml2.2.dylib创建大量内存泄漏
EN

Stack Overflow用户
提问于 2012-03-12 15:48:24
回答 3查看 1.6K关注 0票数 3

在我的项目中实现使用XpathQuery时,我很难删除/避免泄漏。目前,我正在使用libXml2.2.dylib进行解析。当我使用仪器检查内存泄漏时,我在框架中发现了大量的泄漏。

怎样才能避免这些泄漏。记忆是我们更关心的问题。我怎样才能解决/避免这些漏油事故呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-12 22:04:20

首先,如果内存是一个主要问题,那么无论如何使用XML并不是最好的解决方案。JSON或二进制格式的内存效率要高得多。

其次,跟踪不会显示框架中的漏洞。显示的是泄露的对象,这些对象的内存由框架分配。更有可能的是,实际的泄漏出现在您的代码中,通常是从库中分配一个对象,然后不释放(在本例中是释放)对象。看看堆栈的痕迹。

票数 3
EN

Stack Overflow用户

发布于 2012-06-09 13:53:11

我解决了这个问题,当我的文档中有空标记时,.I得到了泄漏,我清楚地观察和修改了一些代码,也就是我在XpathQuery.m中纠正的代码it.Basically。

代码语言:javascript
复制
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query)
{
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj;
    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL)
    {
        return nil;
    }
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((xmlChar *)[query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
    if(xpathObj == NULL) {
        GLogInfo(@"%@",@"Unable to evaluate XPath");
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx); 
        return nil;
    }

    xmlNodeSetPtr nodes = xpathObj->nodesetval;
    if (!nodes)
    {
        GLogInfo(@"%@",@"Nodes was nil.");
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx); 
        return nil;
    }

    NSStringEncoding encoding = cocoaEncodingFromXmlEncodingConst(doc->encoding);
    NSMutableArray *resultNodes = [NSMutableArray array];
    for (NSInteger i = 0; i < nodes->nodeNr; i++)
    {
        NSDictionary *nodeDictionary = DictionaryForNode(nodes->nodeTab[i], nil, encoding);
        if (nodeDictionary)
        {
            [resultNodes addObject:nodeDictionary];
        }
    }

    /* Cleanup */
    xmlXPathFreeObject(xpathObj);
    xmlXPathFreeContext(xpathCtx); 
    return resultNodes;
}

如果您使用的是同一个类,那么您绝对可以使用这个方法,这是毫无疑问的/完全考虑到内存泄漏的。祝好运。

票数 2
EN

Stack Overflow用户

发布于 2016-05-29 19:12:32

如果使用的是TFHpple,则需要在

DictionaryForNode

方法,方法

XPathQuery类

代码语言:javascript
复制
if (XML_ELEMENT_NODE != currentNode->type) {
    return nil;
}

所以你的最后一个方法应该是

代码语言:javascript
复制
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;
}

if (XML_ELEMENT_NODE != currentNode->type) {
    return nil;
}

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);
            return nil;
        }
        if (currentNodeContent != nil) {
            resultForNode[@"nodeContent"] = currentNodeContent;
        }
        return resultForNode;
    } else {
        resultForNode[@"nodeContent"] = currentNodeContent;
    }
    xmlFree(nodeContent);
}

xmlAttr *attribute = currentNode->properties;
if (attribute) {
    NSMutableArray *attributeArray = [NSMutableArray array];
    while (attribute) {
        NSMutableDictionary *attributeDictionary = [NSMutableDictionary dictionary];
        NSString *attributeName = [NSString stringWithCString:(const char *)attribute->name
                                                   encoding:NSUTF8StringEncoding];
        if (attributeName) {
            attributeDictionary[@"attributeName"] = attributeName;
        }

        if (attribute->children) {
            NSDictionary *childDictionary = DictionaryForNode(attribute->children, attributeDictionary, true);
            if (childDictionary) {
                attributeDictionary[@"attributeContent"] = childDictionary;
            }
        }

        if ([attributeDictionary count] > 0) {
            [attributeArray addObject:attributeDictionary];
        }
        attribute = attribute->next;
    }

    if ([attributeArray count] > 0) {
        resultForNode[@"nodeAttributeArray"] = attributeArray;
    }
}

xmlNodePtr childNode = currentNode->children;
if (childNode) {
    NSMutableArray *childContentArray = [NSMutableArray array];
    while (childNode) {
        NSDictionary *childDictionary = DictionaryForNode(childNode, resultForNode,false);
        if (childDictionary) {
            [childContentArray addObject:childDictionary];
        }
        childNode = childNode->next;
    }
    if ([childContentArray count] > 0) {
        resultForNode[@"nodeChildArray"] = childContentArray;
    }
}

xmlBufferPtr buffer = xmlBufferCreate();
xmlNodeDump(buffer, currentNode->doc, currentNode, 0, 0);
NSString *rawContent = [NSString stringWithCString:(const char *)buffer->content encoding:NSUTF8StringEncoding];
if (rawContent != nil) {
    resultForNode[@"raw"] = rawContent;
}
xmlBufferFree(buffer);
return resultForNode;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9670256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档