我的代码有什么问题吗?

Joe*_*Joe -1 objective-c

-(void)processGlyph:(int)glyphOne withGlyph:(int)glyphTwo
{
    answer = glyphOne + glyphTwo;
    NSString *tempText = [[NSString alloc] init];
    tempText = [NSString stringWithFormat:@"%i",answer];
    [self dispatchText:tempText];
    [tempText release];
}

-(void)checkReadyToProcess
{
    if (count >= 2) {
        [self processGlyph:firstGlyph withGlyph:secondGlyph];
    }
}

-(void)dispatchText:(NSString *) theText
{
    answerText.text = theText;
}
Run Code Online (Sandbox Code Playgroud)

sho*_*sti 7

是.是这里:

NSString *tempText = [[NSString alloc] init];//leaked
tempText = [NSString stringWithFormat:@"%i",answer];//creates new autoreleased object
...
[tempText release]; //causes an eventual crash
Run Code Online (Sandbox Code Playgroud)

你正在分配一个NSString,用自动释放替换变量NSString,然后释放自动释放NSString.这将导致内存泄漏(来自原始内容NSString)和过度释放导致的崩溃.

相反,只需:

NSString *tempText = [NSString stringWithFormat:@"%i",answer];
Run Code Online (Sandbox Code Playgroud)

您不必释放它.