下面的代码如预期的那样工作,清理没有僵尸的东西。此方法存在的类是节点的所有者,正在发布,但在“分析”时出现以下两个问题。不正确的递减错误,代码审查
如果可能,你能帮我理解为什么吗?
- (void) dealloc {
NSLog(@"Releasing [Doubly Linked List] .. ");
Node *thisNode = [self firstNode];
Node *nextNode = [thisNode next];
while (nextNode != nil) {
// If "Next node" is not nil, it means that
// "previous node" can now be released
NSLog(@" - releasing node \"%c\"", [[nextNode previous] charData]);
[[nextNode previous] release];
nextNode = [nextNode next];
}
[[self lastNode] release];
[super dealloc];
}
'nextNode'在类中定义,但通过'properties'。这是班级的局部变量。每个节点“知道”前方和后方的内容。类拥有所有节点。 '[nextNode previous]'本身就是'Node *' – JAM
如果previous是一个属性,那么释放它的正确方法是'self.previous = nil;'。不过,我怀疑这是否是这种情况。 – zaph