潜在的内存泄漏
我使用 Xcode 4 在 iPhone iOS 上开发一个项目
。产品 > 分析 我收到 35 个问题,全部都是这种类型:
myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];
问题是“在...分配的对象的潜在泄漏”
什么是有问题的对象以及如何释放它?
谢谢
I work on a project on iPhone iOS with Xcode 4.
With Xcode > Product >Analyze I get 35 issues, all of this type:
myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];
and the problem is "Potential leak of an object allocated at ..."
What is the offending object and how can I release it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在泄漏分配给 myTextField.text 的字符串。发生此分配时,将进行复制(请查看 文档)。 下,当值不可变时(如 NSString 的情况),副本将为您提供一个指向与正在复制的对象相同位置的实例,并且保留计数增加 1。
在大多数情况 code:
您分配的字符串的保留计数为 2。
您需要 (1) 释放(或自动释放)字符串,或 (2) 使用 NSString 便捷方法之一,例如 stringWithFormat: 来创建字符串。这将为您提供一个自动释放的字符串,因此您不必担心显式释放它。
(1)
或
(2)
You're leaking the string that you're assigning to myTextField.text. When this assignment happens, a copy is being made (look at the property definition in the documentation). In most cases, when values are immutable, as is the case with NSStrings, a copy will give you an instance that points to the same location as the object that is being copied, with the retain count incremented by 1.
In the case of your code:
The retain count of the string that you've allocated is 2.
You will either need to (1) release, (or autorelease) the string, or (2) use one of the NSString convenience methods, e.g. stringWithFormat: to create the string. This will give you an autoreleased string so you won't have to worry about explicitly releasing it.
(1)
or
(2)
您负责释放您在此处创建的字符串对象 - 正如您使用 alloc/init 那样。
这里设置字符串最方便的方法是使用返回自动释放字符串的类方法 +stringWithFormat - 这样系统稍后会为您释放该字符串对象:
或者如果您愿意,您可以显式编写自动释放:
如果您不想使用自动释放,可以使用临时变量创建新字符串并在为文本字段设置后释放它:
You are responsible for releasing string object you create here - as you use alloc/init for that.
The most convenient way here to set a string is to use class method
+stringWithFormat
that returns autoreleased string - so system will release that string object for you later:Or you can write autorelease explicitly if you want:
If you don't want to use autorelease you can use temporary variable to create new string and release it after it was set for text field:
问题是 UiTextFields 的 text 属性被声明为:
因此在这一行中:
创建一个新的
NSString
,其保留计数为 1,然后myTextField.text
复制该对象并将其保留计数增加 1 或者是吗??让我们看看发生了什么:NSString
对象,其保留计数为 1NSString
对象是一个副本之前的 String,但由于NStrings
在这种情况下是不可变的,因此复制返回相同的对象!,因此NSString
> 实际上保留计数为 2。The thing is that UiTextFields's text property is declared as:
Therefore in this line:
A new
NSString
is created with a retain count of 1, and thenmyTextField.text
copies this object and increased its retain count by 1 or does it??, lets see what is happening:NSString
object created with alloc initWithFormat with a retain count of 1NSString
object with is a copy of the previous String, but becauseNStrings
are immutable in this case, copy returns the same object!, therefore theNSString
actually has a retain count of 2.