使用 NSString 作为变量会导致 ios 中的内存泄漏,请建议使用更好的数据类型作为变量

发布于 2024-12-12 08:24:04 字数 459 浏览 3 评论 0原文

我正在创建一个计算器应用程序,这是我的第一个 ios 应用程序,所以我不太关心内存管理,但现在我回头看,我发现了一些泄漏。我确切地知道泄漏在哪里(使用 Leaks - XCODE),因为所有泄漏都是由无法修改的 NSString 引起的。因此,我什至为了更改标签的文本而分配字符串:

label.text = [[NSString alloc] initWithFormat: @"%@", number];

其中数字是另一个字符串(我必须对其进行大量修改并再次导致泄漏) 因此,每次我修改标签时,都会发生泄漏。

有人可以建议使用什么数据类型,以便我可以只使用该对象的一份副本并根据需要继续修改它。或者另一种更改标签的方法,编号要么不使用分配,要么及时释放。

我尝试过自动释放它,但它使应用程序崩溃。仅供参考,除了泄漏之外,该应用程序运行良好:)(但我知道它是错误的,因此)

请帮忙!谢谢

I am creating a calculator app and this is my first ios app, so i did not care much about memory management, but now that I look back I see some leaks. I know exactly where the leaks are(used Leaks - XCODE), as all the leaks are caused by NSStrings which cannot be modified. Hence, I have been allocating strings even for changing the text of the label:

label.text = [[NSString alloc] initWithFormat: @"%@", number];

where number is another string(which i have to modify a lot and again causing leaks)
Hence, everytime I modify the label, I have a leak.

Can someone please suggest what datatype to use so that I can just use one copy of the object and keep modifying it as I need. OR another way to change the label,number either w/o using alloc or releasing it in time.

I have tried autoreleasing it but it crasehes the app. FYI, along with the leaks, the app works great :)(but i know its wrong, hence)

Please help! Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

写下不归期 2024-12-19 08:24:04

假设你的标签是 UILabel,那么你的问题很明显,UILabel 的文本属性被定义为保留,当你进行分配时,它返回一个带有保留+1的对象,当你分配给该属性时,setter 将保留计数增加一,因此你有一个+2保留计数,当你摆脱你泄漏的标签时,事情应该看起来像

label.text = [[[NSString alloc] initWithFormat: @"%@", number] autorelease]; 

或者

label.text = [NSString stringWithFormat: @"%@", number];

你应该阅读内存管理指南这里

-丹尼尔

Assuming your label is UILabel, then your problem is obvious, the text property of UILabel is defined as retain, when you do an alloc it returns an object with retain +1 when you assign to the property the setter increases the retain count by one, therefore you have a +2 retain count, and when u get rid of your label you leak, the thing should look like

label.text = [[[NSString alloc] initWithFormat: @"%@", number] autorelease]; 

or

label.text = [NSString stringWithFormat: @"%@", number];

You should read up on memory managment guidelines here

-Daniel

迷途知返 2024-12-19 08:24:04

尝试使用

label.text = [NSString stringWithFormat: @"%@", number];

将文本分配给标签,请改用自动释放的字符串。

顺便问一下...数字是什么?它是一个字符串还是一个整数?

稍后,您可能需要阅读一些有关iOS 上的内存管理。

try using

label.text = [NSString stringWithFormat: @"%@", number];

For assigning text to a label, use an autoreleased string instead.

By the way... what is number? is it a string or is it an integer?

Later on, you might want to read up on some documentation regarding memory management on the iOS.

别把无礼当个性 2024-12-19 08:24:04

请使用它:

label.text = [NSString stringWithFormat: @"%@", number];

stringWithFormat 创建一个自动释放的 NSString,当自动释放池耗尽时,它将为您释放。

Use this instead:

label.text = [NSString stringWithFormat: @"%@", number];

stringWithFormat creates an autoreleased NSString that will be released for you when the autorelease pool is drained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文