基本保留、自动释放问题
保留和自动释放问题。
// A
UIView *temp = [[UIView alloc] init];
myView = temp;
[temp release];
// B
myView = [[UIView alloc] init];
这两个代码没有区别吗?
NSString *str = [NSString stringWithString:@"Hello"];
NSString *str = @"Hello";
还有这两个?我还不确定保留计数。谢谢。
retain and autorelease questions.
// A
UIView *temp = [[UIView alloc] init];
myView = temp;
[temp release];
// B
myView = [[UIView alloc] init];
Do the two codes have no differences?
NSString *str = [NSString stringWithString:@"Hello"];
NSString *str = @"Hello";
And these two? I'm not sure about retain count yet. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于第一个例子,它们非常不同。在第一个代码块中,给予 temp 的 UIView 的保留计数为 1(感谢
alloc
)。当您在第三行释放它时,MyView 变量现在是坏的,因为该对象可能会被销毁。如果您希望 MyView 保留它,请执行以下操作:第一个示例的第二部分将创建一个全新的 UIView 实例,该实例与
temp
没有关系。在第二个示例中,
stringWithString
方法将autorelease
您的字符串,这意味着当“释放池”释放时(很久以后),它将自动为您释放。您不必担心释放它。然而,在第二行中,字符串是静态分配的。对它们来说,保留计数和释放是完全没有必要的。忘了提及...查看此问题的答案 有关保留/释放规则的更多信息。
For the first example, they are very different. In first code chunk, the UIView given to temp has a retain count of 1 (thanks to
alloc
). When you release it on the third line, the MyView variable is now bad, because the object could be destroyed. If you want MyView to keep it, do:The second part of the first example will create an entirely new instance of a UIView, which has no relationship to
temp
.In the second example, the
stringWithString
method willautorelease
your string, meaning that it will be automatically released for you when the "release pool" is released, much later. You don't have to worry about releasing it. In the second line, however, the string is statically allocated. Retain counts and releasing are totally unnecessary with them.Forgot to mention... check out the answer to this question for more about the retain/release rules.
第一部分:不一样!
MyView 也将被释放,因为您只是复制指针(保留计数 0)。
在第二个代码中,MyView 的保留计数为 1。
第二部分:基本相同。
First part: It's not the same!
MyView will be released too, because you're just copying the pointer (retain count 0).
In the second code MyView will have retain count of 1.
Second part: It's basically the same.
请记住,引用 MyView 仅指向 temp。因此,一旦释放 temp,这也会影响 MyView。
[NSString stringWithString:]主要用于复制其他字符串,而不是引用内存地址。例如:
A:
乙:
Remember that the reference MyView just points to temp. So as soon as you release temp, this also affects MyView.
The [NSString stringWithString:] is mainly used for copying other strings instead of referring to the memory address. E.g:
A:
B:
一件有用的事情是,您可以 NSLog 保留计数,以便您可以自己进行测试。
但回到你的问题...
如果 MyView 是一个属性,并且你用 self.MyView 引用了它,并且它是用保留或复制声明的,那么你的两个语句是相同的。如果 MyView 只是一个局部变量,那么你的 UIView 将在你这样做时释放,
因为自从分配它以来你没有做任何事情来增加保留计数。
对于您的字符串示例...
返回一个自动释放的字符串。如果您需要长期保留它,您需要保留它。
第二个字符串示例是静态分配的字符串,您不必担心它。保留计数不适用于它们。
One useful thing, is that you can NSLog the retaincount, so that you can do testing on your own.
But back to your question...
IF MyView is a property, and you had referenced it with self.MyView and it was declared with retain or copy, your 2 statements are the same. If MyView is just a local variable, your UIView will dealloc when you do
because you have done nothing to increase the retain count since you alloced it.
For your string example...
returns an autoreleased string. if you will need to keep it for a very long time, you'll want to put a retain on it.
The second string example is a statically allocated string, and you do not have to worry about it. retain counts don't apply to them.