我们需要释放常量吗?
如何释放在类的实现部分中声明如下的常量:
static NSString *myconst = @"some data...";
谢谢您的帮助,
Stephane
How does a constant declared as below in the implementation part of a class is released:
static NSString *myconst = @"some data...";
Thx for helping,
Stephane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不需要释放使用
@""
创建的字符串。您只需要释放使用alloc
、retain
、copy
或new
创建的对象。No, you don't need to release strings created with
@""
. You only need to release objects created withalloc
,retain
,copy
ornew
.你不必释放它。字符串文字驻留在可执行文件的数据部分,而不是动态分配的内存(也称为堆)中。
不过,意外调用
release
并没有什么坏处。我很确定文字会悄悄地忽略该调用。You don't have to release it. The string literals reside in the executable's data section, not in the dynamically allocated memory (AKA heap).
There's no harm in accidentally calling
release
though. I'm pretty sure the literals are wired to quietly ignore that call.上述类型字符串的保留、释放和自动释放消息将被忽略。
在此处阅读 Apple 的内存管理文档
然而,这里需要注意的一件事是,通过发布会使应用程序崩溃。因此,通常的想法是,如果您没有在字符串上使用 alloc 或 keep,则不要尝试释放它。
另请阅读这个有用的链接此处,它解释了同样的事情。
retain, release and autorelease messages to strings of the above kind are ignored.
Read Apple's memory management docs here
However one thing to note here is passing release crashes the app. Hence the usual idea being, if you haven't used alloc or retain on the string, don't attempt to release it.
Also read this useful link here which explains the same thing.