我注意到在启用ARC的Objective-C中有以下情况:
假设我们有一个简单的类A和自动生成的弱引用属性。
@interface A
@property (nonatomic, weak) id refObject;
@end
@implementation A
@end
并且有实现dealloc方法的B类,属于第二类
@interface B
@end
@implementation B
-(void) dealloc
{
NSLog(@"In dealloc");
}
@end
最后,在A类中有以下内容:
@implementation A
...
-(void) foo
{
B* b = [B new];
self.refObject = b;
// Just use b after the weak assignment
// in order to not dealloc 'b' before assignement
NSLog(@"%@", b);
}
...
@end
如果我在[B dealloc]
处设置断点并检查[A refObject]
属性,我可以看到a.refObject
为nil,但a->_refObject
不为nil,并指向“b”。有任何想法为什么会发生这种情况?
nil
,但实例变量本身保持不变(并被释放,现在它是一个悬空指针)。 - user529758