NSString的内存问题 [英] Memory issue of NSString

查看:72
本文介绍了NSString的内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了NSString问题。

I have got a problem with NSString.

NSString* str = [[NSString alloc] initWithString:@"Hello world"];

在上面的代码中,我是否需要释放对象str?根据规则,此对象是使用alloc创建的,因此应使用release方法显式释放它。但是,当我没有明确释放它时,我在Instruments中找不到任何内存泄漏。
用NSNumber替换NSString时,会发生内存泄漏。

In the code above, do I need to release the object str? According to the rule, this object is created with alloc, so it should be explicitly released with the release method. However, I can't find any memory leaks in Instruments when I don't explicitly release it. When the NSString is replaced with NSNumber, the memory leaks happen.

有人可以给我一些建议吗?谢谢。

Can anyone give me some suggestions? Thanks.

推荐答案

是的,您确实需要释放它。工具,泄漏,ObjectAlloc和朋友之类的工具并非万无一失;同样,因为您使用了常量字符串来创建实例,所以很可能不会发生泄漏。但是,尽管如此,如果您 + alloc 一个对象,还应该 -release 来确保该对象不会

Yes, you do need to release it. Tools such as Instruments, Leaks, ObjectAlloc and friends aren't infallible; also because you've used a constant string to create the instance it is entirely likely that no leak occurs. But nonetheless, if you +alloc an object you should also -release it to make sure that the object doesn't leak, so you do indeed need to release this object.

我通常到可可内存管理文章的链接集: http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html

My usual set of links to Cocoa memory management articles: http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html

这篇关于NSString的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆