objective-c - 如果我尝试发布 CXMLDocument 会发生奇怪的崩溃

标签 objective-c ios cocoa-touch touchxml

我正在使用 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;
}

参见倒数第二行,[xmlDoc release]。我已经注释掉了,因为如果我不这样做它就会崩溃。我究竟做错了什么?谢谢。

最佳答案

您可能需要保留您的字典对象,否则它也会在您释放解析器时被释放。尝试将 [units addObject:dictionary]; 更改为 [units addObject:[dictionary retain]];

另一个想法是将您的 xmlDoc 指针设置为自动释放:

CXMLDocument *xmlDoc = [[[CXMLDocument alloc] initWithData:tempData options:0 error:nil] autorelease];

关于objective-c - 如果我尝试发布 CXMLDocument 会发生奇怪的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6723470/

相关文章:

ios - 添加新字符串后对 UITableVIew 进行排序

iphone - NSDateFormatter 返回不正确的字符串?

ios - "The receipt could not be authenticated"- 应该再次检查吗?

ios - AVAssetExportSession videoComposition 未显示视频中的所有帧

objective-c - 如何获取 "\n"字符后的值?

swift - 隐藏导航栏时更改状态栏颜色

iphone - 具有 clearColor 背景的 UITableView 部分?

ios - Swift - 无法更新 UITabBar barTintColor

ios - UITableViewController 和 UIViewController 的父类(super class)

ios - 从单例类中随时了解 View Controller 类中 TableView 的数据源