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

怎样才能避免这些泄漏。记忆是我们更关心的问题。我怎样才能解决/避免这些漏油事故呢?
发布于 2012-03-12 22:04:20
首先,如果内存是一个主要问题,那么无论如何使用XML并不是最好的解决方案。JSON或二进制格式的内存效率要高得多。
其次,跟踪不会显示框架中的漏洞。显示的是泄露的对象,这些对象的内存由框架分配。更有可能的是,实际的泄漏出现在您的代码中,通常是从库中分配一个对象,然后不释放(在本例中是释放)对象。看看堆栈的痕迹。
发布于 2012-06-09 13:53:11
我解决了这个问题,当我的文档中有空标记时,.I得到了泄漏,我清楚地观察和修改了一些代码,也就是我在XpathQuery.m中纠正的代码it.Basically。
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;
}如果您使用的是同一个类,那么您绝对可以使用这个方法,这是毫无疑问的/完全考虑到内存泄漏的。祝好运。
发布于 2016-05-29 19:12:32
如果使用的是TFHpple,则需要在
DictionaryForNode
方法,方法
XPathQuery类
if (XML_ELEMENT_NODE != currentNode->type) {
return nil;
}所以你的最后一个方法应该是
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;
}https://stackoverflow.com/questions/9670256
复制相似问题