非保留对象:什么时候释放?

发布于 2025-01-05 20:12:08 字数 985 浏览 4 评论 0原文

在初始化方法中,我有以下代码

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

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

发布评论

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

评论(2

时间你老了 2025-01-12 20:12:09

我认为您在这里有点困惑:在两个片段中,您创建了一个 tempButton 对象,但随后将其分配给 self.myButton 。此时,tempButtonself.myButton 都是指向同一对象的指针。现在,大概您正在使用的 myButton @property 是一个强属性,因此通过将 tempButton 分配给它,您可以增加其保留计数,因此,在任一版本的代码中,最后都会有+1的保留计数,并且不会被释放。

假设,如果 myButton 不是一个强属性,那么您的代码中就会出现错误,并且在这两种情况下 tempButton 都会被过早释放和释放。以下是两种情况下会发生的情况:

在您的第一个版本中,由于您获得的 tempButton 来自 initcopy 以外的其他内容方法时,它的保留计数为 +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 to self.myButton. At that point, both tempButton and self.myButton are pointers to the same object. Now, presumably the myButton @property you're using is a strong property, so by assigning tempButton 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 cases tempButton 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 an init or copy 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 an init method. But later on you explicitly release it, bringing its retain count to 0, at which point it is immediately dealloc'ed.

我很OK 2025-01-12 20:12:09

非 init 方法与以下内容完全相同:

UIButton *tempButton = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)] autorelease];

因此,我们的想法是更多地了解自动释放池的工作原理,它在大多数情况下非常有用,但您需要了解它的工作原理,以防您稍后会使用该对象该应用程序。
需要注意的是,当您将临时按钮添加到视图时,该视图将保留它,并在从视图中删除它时释放它,如果您希望查看如何释放/,您可以使用工具并检查对象的保留计数如果你想看到它的实际效果,retain 正在进行中。

the non-init method is exactly the same as:

UIButton *tempButton = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)] autorelease];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文