开发者

iOS App crashing when following code is run

开发者 https://www.devze.com 2023-04-10 16:10 出处:网络
I开发者_C百科 am a beginner, building a calculator to get myself familiar with ObjC. So far my code is running ok, except the following code.

I开发者_C百科 am a beginner, building a calculator to get myself familiar with ObjC.

So far my code is running ok, except the following code.

This function is linked to 5 buttons.

The problem is when i press any of the buttons the first time, the code runs flawlessly, so does the second time, but falls over the third time. it is always the third time that it fails. On screen on the third press the button itself vanishes and program stalls, and then crashes.

-(void) displayOnScreenValue{
[mainDisplay setText:onScreenTextValue];
}

-(void) pressOperatorButton:(id)sender{

    UIButton *pressedOpp = (UIButton *)sender;

    onScreenValue = [onScreenTextValue doubleValue]; //onScreenValue is Double

if (!opJustSet) {

    switch (opSelected) {
        case 0:  //Equal
            //mainDisplay.text = @"=";
            accuValue = onScreenValue;
            break;
        case 1:  //Plus
            //mainDisplay.text = @"+";
            accuValue = accuValue + onScreenValue;

            break;
        case 2:  //Minus
            //mainDisplay.text = @"-";
            accuValue = accuValue - onScreenValue;
            break;
        case 3: //Multiply
            //mainDisplay.text = @"x";
            accuValue = accuValue * onScreenValue;
            break;
        case 4: //Divide
            //mainDisplay.text = @"d";
            accuValue = accuValue / onScreenValue;
            break;

        default:
            break;
    }
    NSNumber* value = [NSNumber numberWithDouble:accuValue];
    onScreenTextValue =[value stringValue];
    [self displayOnScreenValue];
    opJustSet = YES;
    [value release];

}

signChangeSet = NO;
dotUsed = NO;
decimalValue = 0;
opSelected = pressedOpp.tag;

//[self displayOnScreenValue];
[pressedOpp release];
}


In this code:

NSNumber* value = [NSNumber numberWithDouble:accuValue];
onScreenTextValue =[value stringValue];
[self displayOnScreenValue];
opJustSet = YES;
[value release];

You should not release value. You only need to release if you called alloc, a copy method, or if you called retain on it. Most messages return an object that will be autoreleased.

Same goes for this

 [pressedOpp release];

remove it -- you didn't alloc pressedOpp (or retain it)

Also, I don't know what onScreenTextValue is, but it should probably be an @property with (retain). If so, you need to access like this:

 self.onScreenTextValue = [value stringValue];

To get the automatic retaining (in Objective-C self.name and name are not the same thing -- the first one uses the property and the second is raw access to the field, which bypasses the generated setter)

  1. Familiarize yourself with reference counting semantics

    http://www.loufranco.com/blog/files/managing-memory-iphone.html

  2. -or- Upgrade to latest Xcode and use automatic reference counting.

One other simple thing to do is to always run "Build and Analyze" and make sure to fix every single problem. It's really good at finding incorrect releases (in fact, this same code is what's behind automatic reference counting)


You are not managing memory properly. For example, the code

onScreenTextValue =[value stringValue];

assigns to a field using an autoreleased value. You should always retain fields and release them later on.

Also, you are releasing value, which is autoreleased. This means it will be doubly released when the autorelease pool is drained (usually during the event loop).

0

精彩评论

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