Objective-C 保留澄清

发布于 2024-09-24 12:08:28 字数 538 浏览 6 评论 0原文

我正在查看这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-10-01 12:08:28

点符号 (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.

分分钟 2024-10-01 12:08:28

我假设 viewControllers 是一个保留关联值的属性。

@property (nonatomic, retain) NSArray *viewControllers;

基于此,让我们分析一下您的代码片段的保留计数:

// controllers -> retainCount == 0
NSMutableArray *controllers = [[NSMutableArray alloc] init]; // controllers (alloc) -> retainCount++ == +1
for (unsigned i = 0; i < kNumberOfPages; i++) {
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers; // controllers (retained by viewControllers) -> retainCount++ == +2
[controllers release]; // controllers (released) == retainCount-- == +1

稍后......

- (void)dealloc {
    [self.viewControllers release]; // controllers (released) -> retainCount-- == 0 (zero == no leak == no crash by over-release)
    ...
}

I will assume that viewControllers is a property that retains the associated value.

@property (nonatomic, retain) NSArray *viewControllers;

Based on this, let's analyze the retain count on your piece of code:

// controllers -> retainCount == 0
NSMutableArray *controllers = [[NSMutableArray alloc] init]; // controllers (alloc) -> retainCount++ == +1
for (unsigned i = 0; i < kNumberOfPages; i++) {
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers; // controllers (retained by viewControllers) -> retainCount++ == +2
[controllers release]; // controllers (released) == retainCount-- == +1

Later on...

- (void)dealloc {
    [self.viewControllers release]; // controllers (released) -> retainCount-- == 0 (zero == no leak == no crash by over-release)
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文