Is there anything wrong with my code?
-(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;
}
Yes. It is here:
NSString *tempText = [[NSString alloc] init];//leaked
tempText = [NSString stringWithFormat:@"%i",answer];//creates new autoreleased object
...
[tempText release]; //causes an eventual crash
You are allocating an NSString
, replacing the variable with an autoreleased NSString
, and then releasing the autoreleased NSString
. This will lead to a memory leak (from the original NSString
) and a crash from over-releasing.
Instead, just do:
NSString *tempText = [NSString stringWithFormat:@"%i",answer];
You don't have to release it.
精彩评论