Objective-C 保留澄清
我正在查看这段代码:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
稍后...
- (void)dealloc {
[viewControllers release];
...
}
我看到 self.viewControllers 和控制器现在指向相同的分配内存(类型为 NSMutableArray *),但是当我调用 [controllers release] 时,self.viewControllers 并未被释放以及,或者设置 self.viewControllers = controllers 自动保留该内存?
I'm looking at this code:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
Later on...
- (void)dealloc {
[viewControllers release];
...
}
I see that self.viewControllers and controllers now point to the same allocated memory (of type NSMutableArray *), but when I call [controllers release] isn't self.viewControllers released as well, or is setting self.viewControllers = controllers automatically retains that memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
点符号 (
self.foo = bar;
) 相当于调用[self setFoo:bar];
。如果您的属性被声明为保留其值,那么您的视图控制器将在这种情况下保留该数组,并在您设置新值后释放它。The dot-notation (
self.foo = bar;
) equals calling[self setFoo:bar];
. If your property is declared to retain its value, then your viewcontrollers will retain the array in this case, and release it once you set a new value.我假设 viewControllers 是一个保留关联值的属性。
基于此,让我们分析一下您的代码片段的保留计数:
稍后......
I will assume that
viewControllers
is a property that retains the associated value.Based on this, let's analyze the retain count on your piece of code:
Later on...