Iphone memory leak issue?
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 [NSDic开发者_JAVA技巧tionary dictionaryWithDictionary:aFinancialLine];// and here
}
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).
精彩评论