基本保留、自动释放问题

发布于 2024-10-12 23:48:06 字数 309 浏览 6 评论 0原文

保留和自动释放问题。

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

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

发布评论

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

评论(4

林空鹿饮溪 2024-10-19 23:48:06

对于第一个例子,它们非常不同。在第一个代码块中,给予 temp 的 UIView 的保留计数为 1(感谢 alloc)。当您在第三行释放它时,MyView 变量现在是坏的,因为该对象可能会被销毁。如果您希望 MyView 保留它,请执行以下操作:

MyView = [temp retain];

第一个示例的第二部分将创建一个全新的 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:

MyView = [temp retain];

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 will autorelease 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.

檐上三寸雪 2024-10-19 23:48:06

第一部分:不一样!

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.

美人迟暮 2024-10-19 23:48:06
  1. 请记住,引用 MyView 仅指向 temp。因此,一旦释放 temp,这也会影响 MyView。

  2. [NSString stringWithString:]主要用于复制其他字符串,而不是引用内存地址。例如:

A:

NSString *string = someOtherString; // 复制对 someOtherString 的引用;

乙:

NSString *string = [NSString stringWithString: someOtherString]; // 复制另一个字符串。

  1. Remember that the reference MyView just points to temp. So as soon as you release temp, this also affects MyView.

  2. The [NSString stringWithString:] is mainly used for copying other strings instead of referring to the memory address. E.g:

A:

NSString *string = someOtherString; // Copies the reference to someOtherString;

B:

NSString *string = [NSString stringWithString: someOtherString]; // Makes a copy of the other string.

溺深海 2024-10-19 23:48:06

一件有用的事情是,您可以 NSLog 保留计数,以便您可以自己进行测试。

但回到你的问题...

如果 MyView 是一个属性,并且你用 self.MyView 引用了它,并且它是用保留或复制声明的,那么你的两个语句是相同的。如果 MyView 只是一个局部变量,那么你的 UIView 将在你这样做时释放,

[temp release];

因为自从分配它以来你没有做任何事情来增加保留计数。

对于您的字符串示例...

[NSString stringWithString:@"Hello"];

返回一个自动释放的字符串。如果您需要长期保留它,您需要保留它。

第二个字符串示例是静态分配的字符串,您不必担心它。保留计数不适用于它们。

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

[temp release];

because you have done nothing to increase the retain count since you alloced it.

For your string example...

[NSString stringWithString:@"Hello"];

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.

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