I have a tight loop that iterates about 500 times. In every iteration, it will create a few NSDecimalNumber objects to do some arithmetics.
Example – this code snippet is in the for loop. the -decimalNumberByAdding: method creates a new NSDecimalNumber instance and autoreleases it.
resultDecimalNumber = [resultDecimalNumber decimalNumberByAdding:anotherDecimalNumber];
So let me get that right: If the loop is huge, I collect thousands of NSDecimalNumber objects which wait for the whole loop to finish and the method to return, in order to get autoreleased after long time waiting.
How could I prevent a memory overflow? I’ve always tried to use non-autoreleased objects, but in this case it seems I have to live with them.
Imagine this was my loop:
for(i=0, i<500, i++) {
resultDecimalNumber = [resultDecimalNumber decimalNumberByAdding:anotherDecimalNumber];
}
What would I have to add there? Must I create a autorelease pool inside the loop and drain it? Would that make sense?
Yes. You should create your own autorelease pool inside the loop. This is exactly the situation that nested autorelease pools are for.