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

查看:146
本文介绍了使用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. 关于谁拥有一个物体的学术问题,这只是任何人维护<$ p $ c>强对该对象的引用。

在查找应用程序泄漏方面,您可以在分析应用程序时使用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).

如果它没有显示在但是,泄漏似乎你必须决定它是否是一个强大的参考周期(以前称为保留周期),一些简单的逻辑错误(例如视图控制器中的某些循环引用,缓存大型对象等)或某些与Core Foundation相关的问题(除非您小心使用 CFBridgingRelease()或<$ c,否则ARC不承担所有权$ c> __ 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:


  • 用鼠标点击拖动(在6之前的Xcode版本中,你必须按住选项键执行此操作时)突出显示时间线的一部分,以确定要检查的内容。您可能希望专注于分配中的一个峰值。例如,我在我的分配中发现了一个突破并突出显示它(这是一个非常简单的例子,我在 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:

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

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天全站免登陆