2013-02-14 61 views
0

我在使用TouchXML library来解析objective-c中的一些XML。该XML在根元素命名空间,所以我用像CXMLNode对象在TouchXML库的方法:使用带有名称空间映射的[CXMLNode nodesForXPath]的内存崩溃

- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error; 

我的代码使用这种方法来选择一串匹配XPath查询节点,那么对于每个节点,我会执行一些更多的XPath查询来读取一些属性。出于某种原因,第二组查询导致了一个pointer being freed was not allocated错误 - 我无法确定这是从哪里来的。

OK,这里的XML的一个片段:

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.2"> 
<Document> 
    <Placemark> 
    <name>Place 1</name> 
    <description><![CDATA[6-20 Luck Street Eltham 3095]]></description> 
    <Point> 
     <coordinates>145.151138,-37.712663,0.000000</coordinates> 
    </Point> 
    </Placemark> 
    <Placemark> 
    <name>Place 2</name> 
    <description><![CDATA[The Pines Shopping Centre, Reynolds Road Doncaster East 3109]]></description> 
    <Point> 
     <coordinates>145.168620,-37.762135,0.000000</coordinates> 
    </Point> 
    </Placemark> 
    <Placemark> 
     <name>Place 3</name> 
     <description><![CDATA[25 Main Street Greensborough 3088]]></description> 
     <Point> 
      <coordinates>145.102788,-37.702511,0.000000</coordinates> 
     </Point> 
    </Placemark> 
</Document> 
</kml> 

所以我读入一个CMLXmlElement这一点,那么我有这个代码读出每个<标>元素:

_locations = [NSMutableArray array]; 
NSDictionary *mappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://earth.google.com/kml/2.2", @"kmlns", nil]; 
NSError *error = nil; 
for (CXMLNode *node in [element [email protected]"//kmlns:kml/kmlns:Document/kmlns:Placemark" namespaceMappings:mappings error:&error]) 
{ 
    [_locations addObject:[[ONEMapLocation alloc] initWithXmlElement:(CXMLElement *)node]]; 
} 

此代码运行没有问题。但随后,在initWithXmlElement位置的每个对象初始化本身,如:

NSDictionary *namespaceMappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://earth.google.com/kml/2.2", @"kmlns", nil]; 
NSError *error = nil; 
_name = ((CXMLNode*)[[xmlElement nodesForXPath:@"./kmlns:name/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue; 
_description = ((CXMLNode*)[[xmlElement nodesForXPath:@"./description/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue; 
NSString *rawCoordinates = _description = ((CXMLNode*)[[xmlElement nodesForXPath:@"./kmlns:Point/kmlns:coordinates/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue; 
NSArray *coordinateArray = [rawCoordinates componentsSeparatedByString:@","]; 
_latitude = [[coordinateArray objectAtIndex:1] floatValue]; 
_longitude = [[coordinateArray objectAtIndex:0] floatValue]; 

当这段代码运行时,它成功地解析XML文档,但随后约一秒钟后该应用程序崩溃与pointer being freed was not allocated错误。如果我注释掉这些行并将_name_description等设置为虚拟值,则它可以正常工作。

我也试过从XML中取出命名空间,并使用TouchXML库中的方法,不用担心命名空间,它工作正常(虽然我不会有能够编辑的奢侈品现实世界中的XML)。

我知道,很长,很复杂的问题,可能还有其他一些可能的原因,但我确实已经将问题隔离到了这些六条线上。

+1

我其实已经在处理与命名空间选择节点的TouchXML方法追查这一个错误 - 我有一个粗略的修复,我会很快发布。下面的评论提醒我,如果你想要的答案。 – 2013-02-25 11:19:27

+0

我自己现在就发生了这个问题 - 你的修复工作? – Ertebolle 2013-03-12 07:40:25

回答

2

以防万一有人来到这里或类似的问题 - 这听起来像这里描述的问题(https://github.com/TouchCode/TouchXML/issues/11),我刚碰到这个问题。从本质上讲,这是一个EXC_BAD_ACCESS错误,因为xml文档早于其子节点被释放,并且当子节点想要释放它们自己时,它们会崩溃。

我没有挖太深进入TouchXML代码,但TouchXML以下变化似乎来解决这个问题,也不会导致任何内存泄漏(我在探查选中):

CXMLDocument.m

-(void)dealloc 
{ 
    // Fix for #35 http://code.google.com/p/touchcode/issues/detail?id=35 -- clear up the node objects first (inside a pool so I _know_ they're cleared) and then freeing the document 

    @autoreleasepool { 

     //### this is added ### fix for BAD_ACCESS on CXMLNode after releasing doc - get rid of all nodes in nodePool first to make sure they are released before the doc is released 
     NSArray* allObjects = [nodePool allObjects]; 
     for(CXMLNode* child in allObjects) 
     { 
      [nodePool removeObject:child]; 
      [child invalidate]; 
     } 
     //### until here ### 

     nodePool = NULL; 
    } 
    // 
    xmlFreeDoc((xmlDocPtr)_node); 
    _node = NULL; 
    // 
} 

CXMLNode.h

//### add manual dealloc function ### 
-(void)invalidate; // added to prevent BAD_ACCESS on doc release ... 

而且在CXMLNode.m

//### invalidate function added to be able to manually dealloc this node ### 
-(void)invalidate { 
    if (_node) 
    { 
     if (_node->_private == (__bridge void *)self) 
      _node->_private = NULL; 

     if (_freeNodeOnRelease) 
     { 
      xmlFreeNode(_node); 
     } 

     _node = NULL; 
    } 
} 
+0

是的!做得好。 – 2013-09-24 09:52:00

+0

注意:我刚刚发现类似的修复已经在TouchXML源代码中完成,但无论出于何种原因,它尚未发布......可能它不是真正开发出来的。 – TheEye 2013-09-24 10:08:45