xcode ARC 循环保留检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9139619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:12:27  来源:igfitidea点击:

ARC circular retain detection

iphoneobjective-ciosxcodeautomatic-ref-counting

提问by Grimless

I ported some old code over to Objective-C ARC (Automatic Reference Counting) and it seems to work great. Except a rather large, high-level object is not being deallocated when it is popped off of my navigation stack, making me believe I have a retain cycle somewhere that ARC has hidden from me (or at least made difficult to track down). What is the best way to weed out this potential retain cycle and/or what is a good way to determine the cause of a memory leak under ARC? Thanks!

我将一些旧代码移植到 Objective-C ARC(自动引用计数),它似乎工作得很好。除了一个相当大的高级对象在从我的导航堆栈中弹出时没有被释放,这让我相信我在某个地方有一个保留周期,ARC 已经对我隐藏了(或者至少很难追踪)。清除这种潜在的保留周期的最佳方法是什么和/或确定 ARC 下内存泄漏的原因的好方法是什么?谢谢!

回答by Todd J.

I just transitioned an older app to use ARC. Instruments showed no leaks, but the allocations continued to go up. I found that by looking at the live objects for something that I knew should be deleted, I was able to track down the retains without a release. Here are the basic steps:

我刚刚将一个旧的应用程序转换为使用 ARC。仪器显示没有泄漏,但分配继续增加。我发现通过查看活动对象中我知道应该删除的东西,我能够在不发布的情况下追踪保留。以下是基本步骤:

  1. Use the Allocations tool in Instruments
  2. Play with your app for a while (if you know what isn't being released, this goes faster)
  3. Change Statistics to Objects in the Allocations jump bar
  4. Sort by Category and find the class name of your unreleased object
  5. Find a living instance and click the little right arrow next to the memory address
  6. Now you can see the history of retains and releases for an object
  1. 使用仪器中的分配工具
  2. 玩一会儿你的应用程序(如果你知道什么没有发布,这会更快)
  3. 在分配跳转栏中将统计更改为对象
  4. 按类别排序并找到未发布对象的类名
  5. 找到一个活着的实例,然后单击内存地址旁边的小右箭头
  6. 现在您可以查看对象的保留和释放历史记录

Screenshot of object history in Instruments

Instruments 中对象历史的屏幕截图

回答by rob mayoff

The best way is usually to use the Leaks instrument in the Instruments app.

最好的方法通常是在 Instruments 应用程序中使用Leaks 工具

The What's New In Instrumentsvideo from WWDC 2011discusses using Instruments to find retain cycles under ARC, starting about 38 minutes in.

来自WWDC 2011What's New In Instruments视频讨论了使用 Instruments 查找 ARC 下的保留周期,从大约 38 分钟开始。