iPhone内存泄漏问题?

发布于 2024-10-09 20:06:50 字数 645 浏览 4 评论 0原文

这段代码片段内存泄漏,如何修复这个内存泄漏?

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-10-16 20:06:50

正如所写,该代码中没有泄漏

然而,可能发生的情况是,该行代码上分配的 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 an NSDictionary instance (doesn't hurt to do so, though, and it is more defensive).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文