iPhone内存泄漏问题?
这段代码片段内存泄漏,如何修复这个内存泄漏?
-(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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如所写,该代码中没有泄漏。
然而,可能发生的情况是,该行代码上分配的 NSDecimalNumber 正在泄漏,因为它在其他地方被过度保留(或释放不足)。尝试构建和分析和/或在分配工具中打开“跟踪保留事件”。
请注意,您可以只返回
aFinancialLine
而无需创建NSDictionary
实例(不过这样做并没有什么坏处,而且更具防御性)。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 anNSDictionary
instance (doesn't hurt to do so, though, and it is more defensive).