I made the function below to update my game's score labelAtlas. However, SOMETIMES I would get an EXC_BAD_ACCESS after this function has returned. As you can see I am creating the 2 NSMutableString with alloc/init and am releasing them at the end of the function, so I really don't understand what I'm doing wrong. I also tried using the class constructor and autorelease and my app still crashes at the same spot. Any sort of advice would be greatly appreciated.
-(void)updateScore:(int)scoreType
{
int score;
if (scoreType == 0 ) {
score = gameScore;
}
else {
score = g_hiScore;
}
NSMutableString *str = [[NSMutableString alloc] init];
NSMutableString *scoreStr = [[NSMutableString alloc] initWithFormat:@"%d", score];
NSInteger blankLen = 12-[scoreStr length];
for ( int i = 0; i < blankLen; i++ ) {
[str appendString:@":"];
}
[str appendString:scoreStr];
if ( scoreType == 0 ) {
[scoreLabel setString:str]; // update labelAtlas
}
else {
[hiScoreLabel setString:str];
}
NSLog(str);
[str release];
[scoreStr release];
}