带有 ARC 的 iOS 应用程序,查找对象的所有者 [英] iOS app with ARC, find who is owner of an object

查看:22
本文介绍了带有 ARC 的 iOS 应用程序,查找对象的所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 ARC 的应用程序,目前似乎存在一些内存泄漏.谷歌搜索我发现了一些关于如何使用 Inspector 的提示.在那里,我可以看到一些类实例的大量分配,我还可以看到一些关于对象如何分配以及保留计数如何改变的调用堆栈.

I'm writing an App that uses ARC and that seems to have some memory leaks at the moment. Googling I found some hints on how to use the Inspector. In there I can see heaps of allocations of instances of some classes and I can also see some call stack on how the object was allocated and how the retain count got changed.

但是好像看不到完整的调用栈所以不知道到底谁拥有这个对象.在我看来,这个所有者以某种方式没有释放对象(或拥有可疑对象的对象).

But it seems I can't see the complete call stack so I don't know who owns the object in the end. It looks to me that this owner is somehow not releasing the object (or the object that owns the suspected object).

谁能给我一个关于寻找已分配对象的所有者的提示?

Can anybody give me a hint on finding the owner of an allocated object?

另请注意,对象并未标记为泄露",而是已分配.在我看来,随着新对象的不断分配,对象似乎被泄漏了.

Please also note that the objects are not marked as "leaked" but as allocated. To me it seems like the objects are leaked as steadily new objects are allocated.

对于如何最好地继续并找到可疑泄漏的任何进一步帮助表示赞赏.

Any further help on how to best proceed and find the suspected leaks are appreciated.

推荐答案

  1. 就谁拥有"一个对象的学术问题而言,这只是维护对该对象的strong引用的任何人.

就查找应用中的泄漏而言,您可以在分析应用时使用 Instruments 中的泄漏"工具(在 Xcode 的产品"菜单上选择配置").

In terms of finding leaks in your app, you can use the "Leaks" tool in Instruments when you profile the app (choose "Profile" on Xcode's "Product" menu).

如果它没有出现在泄漏"中,那么您似乎必须决定它是否是强引用循环(以前称为保留循环),一些简单的逻辑错误(例如某些循环视图控制器中的引用,缓存大对象等)或一些核心基础相关的问题(ARC 不承担所有权,除非您小心使用 CFBridgingRelease()__bridge_transfer>).

If it's not showing up in "Leaks", though, it seems like you then have to decide whether it is a strong reference cycle (formerly known as a retain cycle), some simple logic error (e.g. some circular reference in view controllers, caching large objects, etc.) or some Core Foundation related problem (ARC doesn't assume ownership unless you're careful about using CFBridgingRelease() or __bridge_transfer).

在使用 Instruments 查找分配的来源方面,对我帮助最大的两个技巧是:

In terms of using Instruments to find the source of the allocations, the two tricks that help me the most are:

  • 用鼠标单击并拖动(在 Xcode 6 之前的版本中,您必须在执行此操作时按住 option 键)以突出显示时间线的一部分,以确定您想要的内容检查.您可能希望专注于您的分配高峰之一.例如,我在我的分配中发现了一个凸起并突出显示了它(这是一个非常简单的例子,我在 viewDidLoad 中创建了一个巨大的数组,但希望它能给你这个想法):
  • Click in drag with your mouse (in Xcode versions prior to 6, you have to hold the option key while you do this) to highlight a portion of the timeline, to identify what you want to inspect. You probably want to focus on one of your spikes in allocations. For example, I found a bump in my allocations and highlighted it as such (this was a ludicrously simple example where I create a huge array in viewDidLoad, but hopefully it give you the idea):

  • 当您通过调用树进行检查时,选择隐藏系统库"以专注于您的代码通常很有用.如果您双击 Instruments 中的方法名称(在我的示例中,此处为 viewDidLoad),Instruments 将向您显示正在执行分配的代码:
  • When you inspect by call tree, it's often useful to select "Hide System Libraries", to focus on your code. And if you double click on the method name in Instruments (in my example, here, it would be viewDidLoad), Instruments will then show you your code that's doing the allocation:

然后您可以双击相关的方法列表,它会将您准确地带到进行分配的代码.

You can then double click on the relevant method listing and it will take you precisely to the code that did the allocation.

虽然这并不能表明您发生了泄漏(即强引用循环的位置或您未能释放它的位置),但这种分析通常可以帮助您追踪泄漏对象的实例化位置,即是追踪问题的第一步.

While this doesn't show you were the leak took place (i.e. where the strong reference cycle or where you failed to release it), but this sort of analysis can often help you track down where the leaked object was instantiated, which is the first step to tracking down the problem.

如果你真的必须弄清楚谁拥有"一个对象(即对象的强引用(或保留)发生在哪里),Xcode 8 有一个新的对象图功能.因此,调试应用程序,然后点击调试栏中的调试内存图"图标(下图中红色圈出).完成此操作后,您可以在左侧选择一个对象,然后您可以看到显示该对象所有权声明的对象图:

If you really must figure out who "owns" an object (i.e. where the object's strong references (or retains) occurred), Xcode 8 has a new object graph feature. So debug the app and then tap the "Debug Memory Graph" icon in the debug bar (circled in red, below). Once you do that you can select an object on the left and you can see the object graph that shows claims of ownership on the object:

以上说明所选图像具有被呈现它的 UIImageView 的强引用,而且 ViewController 也保持强引用.

The above illustrates that the chosen image is has strong references by both the UIImageView in which it is presented, but also the ViewController is maintaining a strong reference, too.

在较早的 Xcode 版本中,配置应用程序以通过 Instruments 运行它并选择记录引用计数"选项.在 Xcode 6 中,它位于最右侧面板的记录设置"选项卡上:

In earlier Xcode versions, profile the app to run it through Instruments and select the "Record reference counts" option. In Xcode 6, this is located on "Record settings" tab in the right most panel:

在 Xcode 5 及更早版本中,您必须单击分配工具旁边的 信息按钮才能请参阅此记录引用计数"选项:

In Xcode 5 and earlier, you have to click on the info button next to the Allocations tool to see this "Record reference counts" option:

无论如何,然后您可以转到分配摘要,深入了解一些未发布的对象(通过单击向右箭头 在分配工具中查看和对象时对象地址旁边),然后您将看到相关对象的保留和释放列表,如上所示.但是,只有在分析应用程序之前, 选择记录引用计数",这才会被捕获.

Anyway, you can then go to the Allocations Summary, drill into some object that wasn't released, (by clicking on the right arrow next to the object address when looking at and object in Allocations tool), and then you'll see the list of retains and releases for the object in question, as shown above. But this only will be captured if you select the "Record reference counts" before profiling the app.

习惯以这种方式跟踪保留计数需要一段时间,但如果您绝对需要知道强引用是在哪里建立的,记录引用计数"选项可以帮助您.

It takes a while to get used to tracking retain counts this way, but if you absolutely need to know where the strong references were established, the "Record reference counts" option can help you out.

这篇关于带有 ARC 的 iOS 应用程序,查找对象的所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆