开发者

Objective C: release and alloc

开发者 https://www.devze.com 2023-03-19 23:00 出处:网络
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, basica开发者_如何转开发lly I was told that when ever you alloc an ob

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, basica开发者_如何转开发lly 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{}


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;


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号