知道保留周期在哪里并删除它们 [英] Knowing where retain cycles are and removing them

查看:117
本文介绍了知道保留周期在哪里并删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法(或至少一种方法)来找出程序中保留循环的位置.另外,如果我随后根据这些类型(例如变量或闭包)知道这些保留循环存在于何处,该如何使它们变弱.我需要使用self(我的GameScene)停止所有保留周期,以便在我不再需要它并希望重新启动它时将其分配.

I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I need to stop all retain cycles with self (my GameScene) so that it deallocates when I don't need it anymore and I want to restart it.

任何提示,建议,答案和反馈,将不胜感激(并且最好提供特定的代码和示例).谢谢.

Any tips, advice, answers, and feedback would be greatly appreciated (and providing specific code and examples would be preferred). Thank you.

@Sweeper的答案正是我想要的.如果您遇到相同的问题,他的回答会有所帮助.谢谢@Sweeper!

@Sweeper's answer was just what I was looking for. If you're having the same issue his answer will help. Thanks @Sweeper!

推荐答案

如果您使用的是Xcode 8或更高版本,则可以使用内存图thing来查看哪个对象持有对该对象的引用.

If you are using Xcode 8 or above, you can use the memory graph thingy to see what object holds a reference to what object.

要查看内存图,请先使用Xcode构建并运行您的应用.如果要检查是否正确丢弃了所有创建的实例,请转到左侧窗格中的以下选项卡:

To see the memory graph, first build and run your app with Xcode. When you want to check whether all the instances you created are discarded properly, go to this tab on the left pane:

然后按右侧的按钮:

然后,选择最底部的选项-查看内存图层次结构":

After that, select the bottom-most option - View Memory Graph Hierarchy:

现在,它将向您显示内存中的所有对象:

Now it will show you all the objects that are in memory:

就我而言,我有一个GameSystem对象,6个ButtonNode对象以及一些其他对象.您会注意到在GameSystem对象旁边有一个!.这意味着该对象已泄漏.另外,由于当前场景为TitleScene,因此GameScene不应再在内存中.让我们通过单击实例来查看保留它的内容:

In my case, I have a GameSystem object, 6 ButtonNode objects and a few others. You'll notice that there is a little ! beside the GameSystem object. That means this object is leaked. Also, GameScene should not be in memory anymore because the current scene is TitleScene. Let's see what is retaining it by clicking on the instance:

现在您可以清楚地看到它被闭包保留了!

Now you can clearly see that it is retained by a closure!

这就是使用内存图查看应在何处放置弱引用并避免保留周期的方法.

That is how you use the memory graph to see where you should put weak-references and avoid retain cycles.

这篇关于知道保留周期在哪里并删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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