一个简单的 Objective C xcode 内存管理问题

发布于 2024-09-15 17:25:01 字数 302 浏览 1 评论 0原文

在下面的代码中,当 tapsMessage 被释放时, tapsLable.text 是否会被释放,或者赋值运算符是否会以某种方式增加保留计数,以便 tapsLabel.text 继续可用?

NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%d taps detected", numTaps];
tapsLabel.text = tapsMessage; // tapsLabel is a UILabel object
[tapsMessage release];

In the code below, will tapsLable.text be deallocated when tapsMessage is released or does the assignment operator somehow increment the retain count so that tapsLabel.text continues to be available?

NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%d taps detected", numTaps];
tapsLabel.text = tapsMessage; // tapsLabel is a UILabel object
[tapsMessage release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

み零 2024-09-22 17:25:01

这里有一个提示

您可以为对象编写retainCounter,然后您可以看到它在赋值之前和之后的情况。

例如 NSLog( @"%d", [tapsMessage keepCount] );

这样您以后就可以通过写出 keepCount 来回答此类问题,因为它始终取决于属性的声明方式。

Here's a tip

You can write the retainCounter for the object then you see what it is before and after the assignment.

e.g. NSLog( @"%d", [tapsMessage retainCount] );

That way you can answer such questions in the future by just writing out the retainCount as it always depends on how the property is declared.

深海蓝天 2024-09-22 17:25:01

tabsLabel.text 是 tapsLabel 的属性。我认为这是一个在赋值时执行[目标复制]的字符串属性。不要介意细节,是的,赋值运算符要么增加保留计数,要么复制值,因此您可以释放 tapsMessage 并且它在 tapsLabel.text 中仍然可用。

在此处了解有关属性的更多信息。

编辑:在标题中查找 UILabel,是的,它会复制文本属性。

@property(nonatomic,copy)   NSString       *text;            // default is nil

编辑:扩展评论中的重要问题

如果您必须查看分配对象的每个对象的实现细节,那么谁知道何时释放、何时不释放?

您只需遵循内存管理规则即可。重新计数环境的要点在于,在内存管理方面,对象之间存在一些“松散耦合”。只要您正确地保留和释放,只要所有相关方的保留/释放都匹配,您就不必关心其他人是否也保留和释放这些相同的对象。

tabsLabel.text is a property on tapsLabel. I think it's a string property that does [target copy] on assignment. Nevermind the details, yes, the assignment operator either increments the retain count or copies the value, so you can release tapsMessage and it is still available in tapsLabel.text.

Read more about properties here.

EDIT: looked up UILabel in the header, yes, it does a copy for the text property.

@property(nonatomic,copy)   NSString       *text;            // default is nil

EDIT: to expand on the important question in the comment

How does anyone know when to release and when not to if you have to look at the implementation details of every object you assign something to?

You just follow the memory management rules. The point of the refcounted environment is exactly that there is some "loose coupling" going on between the objects in terms of memory management. As long as you retain and release properly, it is not your concern whether someone else also retains and releases these same objects, as long as all involved parties have their retains/releases matched.

将军与妓 2024-09-22 17:25:01

在第一行中,您已经分配并初始化了一个 NSString。您根据 内存管理规则,表示你负责释放。

在第二行中,您将 tapsMessage 字符串分配给 text 属性。此属性(假设 tapsLabelUILabel)是使用 copy 属性声明的。对于不可变字符串(NSStrings),请求副本只会增加保留计数,因为不需要创建数据的实际副本(数据永远不会改变)。由于 UILabel 已复制该字符串,因此它也声明了所有权(对象可以有多个所有者)。

在第三行中,您放弃了所有权,但该字符串仍然有一个所有者(标签),因此该对象不会被释放。

In the first line, you have allocated and initialised an NSString. You own this object according to the memory management rules, which means you are responsible for releasing it.

In the second line, you are assigning the tapsMessage string the text property. This property (assuming tapsLabel is a UILabel) is declared with the copy attribute. For immutable strings (NSStrings), asking for a copy simply increments the retain count, since there is no need to make an actual duplicate of the data (the data can never change). Since UILabel has made a copy of the string, it has claimed ownership as well (objects can have more than one owner).

In the third line, you relinquish your ownership but the string still has one owner (the label), so the object is not deallocated.

¢好甜 2024-09-22 17:25:01

它不会被释放。

It will not be deallocated.

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