[英]Over-releasing an NSDecimalNumber
My app is telling me that I'm over-releasing the NSDecimalNumber tempDouble below: 我的应用程序告诉我,我在下面过度释放了NSDecimalNumber tempDouble:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setLocale:[NSLocale currentLocale]];
[currencyFormatter setGeneratesDecimalNumbers:TRUE];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
// Here is the key: use the maximum fractional digits of the currency as the scale
int currencyScale = [currencyFormatter maximumFractionDigits];
NSDecimalNumber* tempDouble = [[NSDecimalNumber alloc] initWithString:self.tempStore];
NSDecimalNumber* numTen = [[NSDecimalNumber alloc] initWithInt:10];
tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];
[numTen release];
[textField setText:[currencyFormatter stringFromNumber:tempDouble]];
[currencyFormatter release];
[tempDouble release];
I'm thinking that the problem line is this one: 我认为问题线是这样的:
tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];
But I'm not sure why. 但是我不确定为什么。 Should I be adding an 'assign, copy or retain' attribute after the assignment?
分配后是否应该添加“分配,复制或保留”属性? When I get rid of the 'release' statement below, the code works fine.
当我摆脱下面的“ release”语句时,代码可以正常工作。
Thanks, 谢谢,
G G
The problem is indeed in this line: 问题确实在此行中:
tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];
What you're doing here is replacing the previous value in tempDouble (which has a retain count of 1) with a new value (which is autoreleased). 您在此处执行的操作是用新值(自动释放)替换tempDouble中的先前值(保留计数为1)。
Here's the correct way to do it: 这是正确的方法:
NSDecimalNumber* tempDouble = [[[NSDecimalNumber alloc] initWithString:self.tempStore] autorelease];
And then delete the [tempDouble release]
from the end of the method. 然后从方法末尾删除
[tempDouble release]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.