一个简单的 Objective C xcode 内存管理问题
在下面的代码中,当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有一个提示
您可以为对象编写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.
tabsLabel.text 是 tapsLabel 的属性。我认为这是一个在赋值时执行[目标复制]的字符串属性。不要介意细节,是的,赋值运算符要么增加保留计数,要么复制值,因此您可以释放 tapsMessage 并且它在 tapsLabel.text 中仍然可用。
在此处了解有关属性的更多信息。
编辑:在标题中查找 UILabel,是的,它会复制文本属性。
编辑:扩展评论中的重要问题
您只需遵循内存管理规则即可。重新计数环境的要点在于,在内存管理方面,对象之间存在一些“松散耦合”。只要您正确地保留和释放,只要所有相关方的保留/释放都匹配,您就不必关心其他人是否也保留和释放这些相同的对象。
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.
EDIT: to expand on the important question in the comment
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.
在第一行中,您已经分配并初始化了一个 NSString。您根据 内存管理规则,表示你负责释放。
在第二行中,您将
tapsMessage
字符串分配给text
属性。此属性(假设tapsLabel
是UILabel
)是使用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 thetext
property. This property (assumingtapsLabel
is aUILabel
) is declared with thecopy
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). SinceUILabel
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.
它不会被释放。
It will not be deallocated.