我在使用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)。
我知道,很長,很複雜的問題,可能還有其他一些可能的原因,但我確實已經將問題隔離到了這些六條線上。
我其實已經在處理與命名空間選擇節點的TouchXML方法追查這一個錯誤 - 我有一個粗略的修復,我會很快發佈。下面的評論提醒我,如果你想要的答案。 – 2013-02-25 11:19:27
我自己現在就發生了這個問題 - 你的修復工作? – Ertebolle 2013-03-12 07:40:25