Knowing where retain cycles are and removing them
Asked Answered
G

1

9

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.

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

Graphy answered 15/1, 2017 at 11:16 Comment(3)
Here you have pretty much answered all about your current issue : https://mcmap.net/q/1170036/-swift-3-spritekit-reseting-gamescene-doesn-39-t-deallocate Also, answer to this question would be quoting Apple documentation , ARC - "Strong Reference Cycles Between Class Instances" section, part with Person & Apartment.Turne
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.Doorway
@Turne Sweeper's answer was what I was wanting to know from this answer not that. Thanks for your response anywayGraphy
T
33

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.

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:

enter image description here

Then press the button on the right there:

enter image description here

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

enter image description here

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

enter image description here

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:

enter image description here

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.

Tiresias answered 15/1, 2017 at 12:51 Comment(1)
Nice answer... I completely forgot about this because I was like forever on Xcode 7.x.x and just recently switched to newest version. Seems that works pretty well when it comes to leaks detection. Also, it sucks that this doesn't work on Swift 2.3 projectsTurne

© 2022 - 2024 — McMap. All rights reserved.