打开下面这段代码工作得很好,如果在项目属性中选择垃圾收集没有打开。但随着GC打开,这是错误消息:XML元素插入错误的
“*断言失败 - [NSXMLFidelityElement insertChild:atIndex:],/SourceCache/Foundation/Foundation-751.53/XML.subproj/XMLTypes.subproj/NSXMLElement .M:823" ‘不能添加具有父项的子;分离或复制第一’
什么建议吗?
-(void)insertXmlRecord
{
//xmlDoc is an iVar
NSXMLElement *nodeToAdd = [[NSXMLElement alloc] initWithXMLString:[self readOnScreenSetAttrib] error:nil];
NSError *err=nil;
NSXMLElement *thisName;
NSArray *nodes = [xmlDoc nodesForXPath:@"./dream" error:&err];
NSLog(@"insertXMLRecord xmldoc %@", xmlDoc);
if ([nodes count] > 0)
{
thisName = [nodes objectAtIndex:0];
NSLog(@"insertXMLRecord: thisname: %@", thisName);
NSLog(@"insertXMLRecord: nodeToAdd: %@", nodeToAdd);
[thisName addChild:nodeToAdd];
}
//NSLog(@"insertXMLRecord");
}
就是这样。仍然需要了解更多关于内存管理的知识! – user523234 2011-05-18 16:59:29
这看起来像一个错误。如果没有GC'nodeToAdd'没有父,与GC它有一个...使用'copy'只是规避错误。所以,除非我在这里是完全关闭基地,我建议你的文件与苹果的bug报告。 – CRD 2011-05-18 19:30:26
我怀疑我们真的没有完整的代码 - 如果父节点被释放但节点被保留,它可能会将其子节点的父节点设置为'nil',但由于引用不是'__weak',它在GC环境中不会这样做。 – 2011-05-18 20:07:04