0

The memory is leaking in this code fragment, how to fix this memory leak ?

-(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
}
pradeep
  • 3,005
  • 12
  • 42
  • 65

1 Answers1

0

As written, there isn't a leak in that code.

What may be happening, though, is the NSDecimalNumber allocated on that line of code is being leaked because it is being over-retained (or under-released) somewhere else. Try build-and-analyze and/or turn on 'track retain events' in the allocations instrument.

Note that you could just return aFinancialLine without creating an NSDictionary instance (doesn't hurt to do so, though, and it is more defensive).

bbum
  • 162,346
  • 23
  • 271
  • 359