【发布时间】:2013-08-06 17:56:55
【问题描述】:
考虑以下两种情况:
// case 1
NSObject *strongOne = [[NSObject alloc] init];
NSObject * __weak weakOne = strongOne;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
strongOne = nil;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
输出这个:
weakOne is not nil.
weakOne is not nil.
和
// case 2
NSObject *strongOne = [[NSObject alloc] init];
NSObject * __weak weakOne = strongOne;
strongOne = nil;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
输出这个:
weakOne is nil.
据我所知,当strongOne被释放时,对同一对象的弱引用应该更新为nil。
我的问题:为什么这只发生在case 2?
【问题讨论】:
-
您还应该使用 Release-build 而不是 Debug 进行测试。结果可能会有所不同...
标签: objective-c pointers automatic-ref-counting weak-references