I am trying to format an array of numbers to display in an array of CCLabelTTFs in a loop. NSNumberFormatter returns an autorelease object. I am not quite sure how I should be handling the memory management here. Any advice would be greatly appreciated.
Here is the problem I’m trying to figure out:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSString *tempString;
for(int i = 0; i<10; i++){
tempString = [formatter stringFromNumber:[NSNumber numberWithInteger:NumberArray[i]]];
CCLabelTTFArray[i] = [[CCLabelTTF alloc] initWithString: tempString fontName:@"Arial" fontSize:10.0f];
[tempString release];
}
[formatter release];
You do not need to call
[tempString release];: the run loop will “autorelease” the objects for you some time after your method completes. With short-running loops (such as yours, that have only ten iterations) there is nothing else to worry about. For loops with thousands of iterations, you may consider adding an Autorelease Pool.Each invocation of the
stringFromNumber:method adds its return value to autorelease pool, so no matter how many times you loop executes, all returned strings will be autoreleased.