非保留对象:什么时候释放?
在初始化方法中,我有以下代码
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(0,0,300,44);
// some custom code...
self.myButton = tempButton;
}
return self;
}
,其中 myButton
是保留的属性。 我知道,对于涉及内存管理规则的问题,此方法等于其他方法:
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)];
// some custom code...
self.myButton = tempButton;
[tempButton release];
}
return self;
}
但在这种情况下,我需要使用第一个“版本”,因为 buttonType
属性是只读的,并且在拥有后我无法更改它按钮已初始化。
由于我发现自己在整个应用程序的多个方法中使用“非 init-release”版本,并且对于多个对象(其中大多数是 NSString
),我的问题是:在这种情况下不计算分配给保留对象的属性,tempButton
对象何时被释放?也许在方法/if 语句的末尾?或者第一个“版本”会导致内存使用量增加,因为对象不是立即释放而是在一段时间后释放?
Inside an initialization method, I have the following code
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(0,0,300,44);
// some custom code...
self.myButton = tempButton;
}
return self;
}
Where myButton
is a retained property.
I know that, for what concerns memory management rules, this method equals this other:
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)];
// some custom code...
self.myButton = tempButton;
[tempButton release];
}
return self;
}
But in this case I need to use the first "version" because the buttonType
property is readonly and I cannot change it after having the button initalized.
Since I find myself using the "non init-release" version in multiple methods all over my application, and for several object (most of them are NSString
), my question is: not counting in this case the assignment to the property which retains the object, when the tempButton
object will be released? Maybe at the end of the method/if statement? Or will the first "version" lead to an increased memory usage, since the object is not being released right away but after a certain amount of time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您在这里有点困惑:在两个片段中,您创建了一个
tempButton
对象,但随后将其分配给self.myButton
。此时,tempButton
和self.myButton
都是指向同一对象的指针。现在,大概您正在使用的myButton
@property
是一个强属性,因此通过将tempButton
分配给它,您可以增加其保留计数,因此,在任一版本的代码中,最后都会有+1的保留计数,并且不会被释放。假设,如果
myButton
不是一个强属性,那么您的代码中就会出现错误,并且在这两种情况下tempButton
都会被过早释放和释放。以下是两种情况下会发生的情况:在您的第一个版本中,由于您获得的
tempButton
来自init
或copy
以外的其他内容方法时,它的保留计数为 +1,但会自动释放。在运行循环的当前迭代结束时,自动释放将启动,将其保留计数变为 0 并导致其被释放。在第二个版本中,您首先获得一个保留计数为 1 的
tempButton
,因为它来自init
方法。但稍后您显式释放它,使其保留计数为 0,此时它立即被释放。I think you're a bit confused here: in both of your snippets, you create a
tempButton
object, but then you're assigning it toself.myButton
. At that point, bothtempButton
andself.myButton
are pointers to the same object. Now, presumably themyButton
@property
you're using is a strong property, so by assigningtempButton
to it, you increase its retain count, and therefore in either version of the code it would have a retain count of +1 at the end, and would not be dealloc'ed.If, hypothetically,
myButton
wasn't a strong property, then there would be an error in your code, and in both casestempButton
would be prematurely released and dealloc'ed. Here's what would happen in the two cases:In your first version, since you're getting
tempButton
comes from something other than aninit
orcopy
method, it gets a retain count of +1, but is autoreleased. At the end of the current iteration of the run loop, the autorelease would kick in, bringing its retain count to 0 and causing it to be dealloc'ed.In the second version, you first get a
tempButton
with a retain count of 1 because it's coming from aninit
method. But later on you explicitly release it, bringing its retain count to 0, at which point it is immediately dealloc'ed.非 init 方法与以下内容完全相同:
因此,我们的想法是更多地了解自动释放池的工作原理,它在大多数情况下非常有用,但您需要了解它的工作原理,以防您稍后会使用该对象该应用程序。
需要注意的是,当您将临时按钮添加到视图时,该视图将保留它,并在从视图中删除它时释放它,如果您希望查看如何释放/,您可以使用工具并检查对象的保留计数如果你想看到它的实际效果,retain 正在进行中。
the non-init method is exactly the same as:
so the idea is to understand more about how the auto release pool works, its very useful most of the time but u need to understand how it works incase u will use the object later on in the app.
and to note something, when u add the temp button to ur view that view will retain it, and will release it when its removed from it, u can use instruments and check the retain count of the object if u wish to view how release/retain is going on if u want to see it in action.