【发布时间】:2011-07-17 11:14:58
【问题描述】:
我正在使用 TouchXML 解析一些 XML,但我遇到了崩溃 -EXC_BAD_ACCESS。我通过反复试验发现,如果我不发布我的 CXMLDocument(我分配的),那么一切都很好。这是我的代码:
- (NSArray *)getLookUps {
//Do some stuff and then...
NSData *tempData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:nil];
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithData:tempData options:0 error:nil];
NSDictionary *mappings = [NSDictionary dictionaryWithObject:@"http://****/****"
forKey:@"****"];
NSLog(@"%@", [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding]);
NSArray *orders = [[xmlDoc rootElement] nodesForXPath:@"//****:Unit"
namespaceMappings:mappings
error:nil];
NSMutableArray *units = [NSMutableArray arrayWithCapacity:200];
for (CXMLElement *order in orders) {
NSArray *nodes = [order children];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:[nodes count]];
for (CXMLElement *node in nodes) {
[dictionary setObject:[node stringValue] forKey:[node name]];
}
[units addObject:dictionary];
}
//[xmlDoc release];
return units;
}
请参阅第 2 行的最后一行,[xmlDoc release]。我已经对此发表了评论,因为如果我不这样做,它就会崩溃。我究竟做错了什么?谢谢。
【问题讨论】:
-
在某些时候,您无法保留或过度发布某些内容。泄漏 CXMLDocument 只是隐藏了问题。你如何处理这个方法返回的数组?你能显示调用这个方法的代码吗?
标签: objective-c ios cocoa-touch touchxml