【发布时间】:2011-07-28 22:02:08
【问题描述】:
我正在测试下面的代码。 ffv 在接口文件中声明。
ffv = [[FullFunctionView alloc] initWithFrame:self.view.bounds];
NSLog(@"%i", [ffv retainCount]); // prints 1
[self.view insertSubview:ffv belowSubview:switchViewsBtn];
NSLog(@"%i", [ffv retainCount]); // prints 2
[ffv release]; // you can release it now since the view has ownership of ffv
NSLog(@"%i", [ffv retainCount]); // prints 1
if (ffv == nil)
NSLog(@"ffv is nil");
// "ffv is nil" is not printed
[ffv testMethod]; // "test method called" is printed
这是我的 [ffv testMethod] 实现
- (void)testMethod
{
NSLog(@"test method called");
}
我在这种情况下的推断是,即使您释放了一个保留计数为 2 的对象,您也会失去该对象的所有权,但是仍然会保留该引用。
现在,我的问题是:
- 我的推论正确吗?
- 还有什么重要的可以从中推断出来的吗?
- 仍然保留(使用)ffv 并从 ffv 调用方法会导致哪些并发症? (我的观点是这没关系,因为视图将始终拥有 ffv,并且在有人调用 viewDidUnload 之前不会释放它。只要我不将 ffv 的引用传递给其他对象。)
【问题讨论】:
-
不敢相信还没有人提到 ARC。我建议阅读它。
-
tnx 人。 clang.llvm.org/docs/AutomaticReferenceCounting.html 我认为这是一本好书。
标签: iphone objective-c ios pointers memory-management