Objective C:释放和分配

发布于 2024-11-19 18:53:46 字数 549 浏览 3 评论 0原文

我对 C 和 Objective C 很陌生,但是我找不到这个答案

,所以我遇到了一个问题,花了我一段时间才解决,基本上我被告知,当你分配一个对象时,你应该释放它。这就是我所做的,它导致我的程序崩溃...

这是代码:

NSString *numberString = [[NSString alloc] init];
numberString = resultLabel.text;
[self setFirstNumber:[numberString doubleValue]];
[resultLabel setText:@"0"];
[numberString release];

我想我明白为什么这是因为“numberString = resultLabel.text”行,但是我不明白为什么程序崩溃。为什么我不能释放numberString?如果不这样做会导致内存泄漏吗?

PS 我知道代码很笨拙,我是编程新手,更是 Objective C 的超级新手。

PSS 我稍后在 -(void)dealloc{} 中发布 resultLabel

I'm pretty new to C and Objective C, however I can't find this answer

So I ran into a problem that took me a while to work out, basically I was told that when ever you alloc an object you should release it. So that is what I did and it caused my program to crash...

Here is the code:

NSString *numberString = [[NSString alloc] init];
numberString = resultLabel.text;
[self setFirstNumber:[numberString doubleValue]];
[resultLabel setText:@"0"];
[numberString release];

I think I figured out why it's because of the "numberString = resultLabel.text" line, however I don't understand why the program crashes. Why can't I release numberString? Will it cause a memory leak if I don't?

P.S. I know the code is clumsy, I'm a newbie to programming and a even more super newbie to Objective C.

P.S.S. I release resultLabel later on in the -(void)dealloc{}

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

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

发布评论

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

评论(3

病女 2024-11-26 18:53:46

numberString 是一个指针,这意味着它将指向您为其分配的内存。在第一行,是的,您确实调用了 alloc/init 并且您负责释放它。在下一行中,尽管您将指针设置为您不拥有的另一个值,但您调用 alloc 的原始字符串正在泄漏。调用 [[NSString alloc] init] 是毫无意义的,但这是您使用发布的示例。

NSString *numberString = [[NSString alloc] init]; //Not necessary
[numberString release]; //Properly released now it is safe to reassign
numberString = resultLabel.text; 
//numberString is now pointing at another object

[self setFirstNumber:[numberString doubleValue]];
[resultLabel setText:@"0"];
//No need to release it any more here

您要做的只是将 numberString 设置为文本,而不使用任何释放调用。

NSString *numberString = resultLabel.text;

numberString is a pointer which means it will point to the memory that you assign it. On the first line yes you did call alloc/init and you are responsible for releasing it. On the next line though you set the pointer to another value that you do not own and the original string you called alloc on is getting leaked. A call to [[NSString alloc] init] is quite pointless but here is your example working with release.

NSString *numberString = [[NSString alloc] init]; //Not necessary
[numberString release]; //Properly released now it is safe to reassign
numberString = resultLabel.text; 
//numberString is now pointing at another object

[self setFirstNumber:[numberString doubleValue]];
[resultLabel setText:@"0"];
//No need to release it any more here

What you will want to do is just set numberString to the text and not use any release calls.

NSString *numberString = resultLabel.text;
尸血腥色 2024-11-26 18:53:46

将前两行更改为:

NSString *numberString = [[NSString alloc] initWithString: resultLabel.text];

由于 NSString 的实例是不可变的,因此在初始化后无法设置其值。您在代码中所做的就是设置指针。

Change the first two lines to:

NSString *numberString = [[NSString alloc] initWithString: resultLabel.text];

As an instance of NSString is immutable you cannot set its value after you have initialized it. What you are doing in your code is setting the pointers.

负佳期 2024-11-26 18:53:46

你说得对!通过编写:

numberString = resultLabel.text;

您现在正在引用 resultLabel.text 的 String 实例,当您尝试释放它时,这是一个错误,因为该 String 尚未由您分配,因此您不能释放它。

如果您只是引用 resultLabel 后面的对象中的文本,则只需省略第一行,因为您只需要一个 NSString(指针)类型的变量,并且不需要 NSString 的新实例。

[编辑] 正如 dasdom 正确提到的那样,通过在第一行分配和实例化一个 NSString 对象,您从不使用该对象,也从不释放该对象(您尝试从正在其他地方释放的标签中释放 NSString),您创建了一个内存泄露。

You're right! By writing:

numberString = resultLabel.text;

You are now referencing the String instance of resultLabel.text and when you're trying to release it, it's an error, because that String has not been allocated by you, so you must not release it.

If you're just referencing the text from the Object behind resultLabel just leave out the first line, because you just need a variable of type NSString (pointer) and you do not need a new instance of NSString.

[EDIT] As dasdom correctly mentioned, by allocating and instancing a NSString object in the first line, which you never use and which you never release (you try to release the NSString from the label which is beeing release somewhere else) you created a memory leak.

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