了解保留循环的位置并删除它们 [英] Knowing where retain cycles are and removing them

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

问题描述

我想知道是否有一种简单的方法(或至少是一种方法)来找出程序中存在保留循环的位置.此外,如果我知道这些保留周期存在于何处,取决于它们的类型(例如变量或闭包),我如何使它们变弱.我需要使用 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 或更高版本,你可以使用 memory graph thingy 来查看什么对象持有对什么对象的引用.

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:

然后按下右侧的按钮:

之后,选择最底部的选项 - View Memory Graph Hierarchy:

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 对象旁边有一个小 !.这意味着这个对象被泄露了.此外,GameScene 不应再在内存中,因为当前场景是 TitleScene.让我们点击实例看看是什么保留了它:

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