此代码片段中的内存是否泄漏,如何修复此内存泄漏?
-(NSDictionary *)sanitizedFinancialLine:(NSDictionary *)theFinancialLine
{
NSMutableDictionary *aFinancialLine = [NSMutableDictionary dictionaryWithDictionary:theFinancialLine];
for (id key in [aFinancialLine allKeys]) {
id something = [aFinancialLine objectForKey:key];
if ([something respondsToSelector:@selector(decimalValue)]) {
something = [NSDecimalNumber decimalNumberWithDecimal:[(NSNumber *)something decimalValue]]; // memory is leaking here
[aFinancialLine setObject:something forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:aFinancialLine];// and here
}
答案 0 :(得分:0)
如上所述,该代码中没有泄漏。
可能会发生的事情是,该行代码上的NSDecimalNumber
已分配正在泄露,因为它在其他地方被过度保留(或未充分释放)。尝试在分配工具中构建和分析和/或启用“跟踪保留事件”。
请注意,您可以在不创建aFinancialLine
实例的情况下返回NSDictionary
(但这样做并没有伤害,而且更具防御性。)