潜在的内存泄漏

发布于 2024-11-27 06:29:32 字数 239 浏览 0 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(3

冰葑 2024-12-04 06:29:32

您正在泄漏分配给 myTextField.text 的字符串。发生此分配时,将进行复制(请查看 文档)。 下,当值不可变时(如 NSString 的情况),副本将为您提供一个指向与正在复制的对象相同位置的实例,并且保留计数增加 1。

在大多数情况 code:

myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

您分配的字符串的保留计数为 2。

您需要 (1) 释放(或自动释放)字符串,或 (2) 使用 NSString 便捷方法之一,例如 stringWithFormat: 来创建字符串。这将为您提供一个自动释放的字符串,因此您不必担心显式释放它。

(1)

NSString *str = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = str;
[str release]

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

(2)

myTextField.text = [NSString stringWithFormat:@"0.2f", abc]; // autoreleased

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:

myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

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)

NSString *str = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = str;
[str release]

or

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

(2)

myTextField.text = [NSString stringWithFormat:@"0.2f", abc]; // autoreleased
舂唻埖巳落 2024-12-04 06:29:32

您负责释放您在此处创建的字符串对象 - 正如您使用 alloc/init 那样。

这里设置字符串最方便的方法是使用返回自动释放字符串的类方法 +stringWithFormat - 这样系统稍后会为您释放该字符串对象:

myTextField.text = [NSString stringWithFormat:@"0.2f", abc];

或者如果您愿意,您可以显式编写自动释放:

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

如果您不想使用自动释放,可以使用临时变量创建新字符串并在为文本字段设置后释放它:

NSString *tempString = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = tempString;
[tempString release];

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:

myTextField.text = [NSString stringWithFormat:@"0.2f", abc];

Or you can write autorelease explicitly if you want:

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

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:

NSString *tempString = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = tempString;
[tempString release];
青衫负雪 2024-12-04 06:29:32

问题是 UiTextFields 的 text 属性被声明为:

@property(nonatomic, copy) NSString *text

因此在这一行中:

 myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

创建一个新的 NSString ,其保留计数为 1,然后 myTextField.text 复制该对象并将其保留计数增加 1 或者是吗??让我们看看发生了什么:

  1. 使用 alloc initWithFormat 创建的 NSString 对象,其保留计数为 1
  2. 一个 NSString 对象是一个副本之前的 String,但由于 NStrings 在这种情况下是不可变的,因此复制返回相同的对象!,因此 NSString > 实际上保留计数为 2。

The thing is that UiTextFields's text property is declared as:

@property(nonatomic, copy) NSString *text

Therefore in this line:

 myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

A new NSString is created with a retain count of 1, and then myTextField.text copies this object and increased its retain count by 1 or does it??, lets see what is happening:

  1. A NSString object created with alloc initWithFormat with a retain count of 1
  2. A NSString object with is a copy of the previous String, but because NStrings are immutable in this case, copy returns the same object!, therefore the NSString actually has a retain count of 2.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文