Swift / SpriteKit - Can't break strong reference cycle

hamobi

I completed a spritekit game without having knowledge of strong reference cycles. I'm not having any crashes or obvious problems, but using instruments I can see my objects being retained between each level (SKScene should be dealloced between levels).

Here are the general pieces of my game:

GameConfig static struct

I have a globally accessible static struct that references my ViewController. I access the viewController using this struct from all over the app. I assign GameConfig.viewController = self inside of the viewController's viewDidLoad

GameViewController

In here I reference my SKScene, and SKView as such

var skView: MyView!
weak var scene: SKScene!

BaseScene

This is my base SKScene class. This is the only property of this class that gets a reference to the class itself

let indicators: Indicators = Indicators()

KillScene subclass of BaseScene

In my BaseScene subclass, I have many custom subclasses contained in arrays. These custom subclasses do reference the scene, but the scene is not referencing them directly. It only has reference to their container array. Example:

var radarBlips: [RadarBlip] = []

I do have one property that has a strong reference cycle with KillScene

var ship: KillShip = KillShip()

In both my Indicators and KillShip classes, I have referenced my scene using the weak keyword.

In fact I have found every instance where I reference the scene, and put weak in front of it.

I tried to make a stripped down game, and create a strong reference cycle between a sprite and a scene, and then break it. I was successful in doing so. But I can't seem to debug my own game.

Any detailed suggestions would be helpful. I know there's trickiness with passing closures around. Maybe that has something to do with it? Any advice on what I could look for would be great. I've put weak in more places than I probably need to.

I can not get the scene to deinit

hamobi

There wasn't an easy way to do this. Check all your properties that reference eachother and use weak or unowned. The bigger issue in spritekit is checking for closures. When you're running delayed closures (in SKActions) and things like that it's important to use [weak self] within the closure so it doesnt keep the object alive. It's easier to add more weaks and unowned than necessary until the strong reference cycle is broken and then work your way backwards deleting unnecessary ones.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift / SpriteKit - Can't break strong reference cycle

From Dev

break strong reference cycle between Managed Object and Collectionview Cell

From Dev

Avoid strong reference cycle in Block, can I simply do this

From Dev

Nested callback strong reference cycle

From Dev

Android: Strong reference cycle using ArrayAdapter

From Dev

Is this a strong reference cycle or memory leak at all?

From Dev

Strong reference cycle: closures vs methods

From Dev

Android: Strong reference cycle using ArrayAdapter

From Dev

How to break strong reference cycles in TypeScript?

From Dev

Swift class: reference cycle

From Dev

How to break a direct reference cycle in CPython

From Dev

How to break a direct reference cycle in CPython

From Dev

Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

From Dev

How to keep a strong reference inside of a block without having a retain cycle

From Dev

OK to use underscore for autosynthesized properties in block (strong reference cycle)

From Dev

How to keep a strong reference inside of a block without having a retain cycle

From Dev

Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

From Dev

Does [self new] in a block creates strong reference cycle?

From Dev

Swift SpriteKit, Can't fire bullet more than once

From Dev

How to break reference cycle between view controller and data source

From Dev

How to break reference cycle between view controller and data source

From Dev

How to reference UILabel strong reference programmatically using Swift?

From Dev

Swift + Storyboards: How to keep strong reference to show segue controller?

From Dev

Why Swift does not dispose circular references (strong reference cycles) automatically

From Dev

convert the reference back to a strong one inside the closure, memory management, swift

From Dev

Can't get retain cycle

From Dev

Can't reference Xcode GameScene.sks with swift

From Dev

Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

From Dev

Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

Related Related

  1. 1

    Swift / SpriteKit - Can't break strong reference cycle

  2. 2

    break strong reference cycle between Managed Object and Collectionview Cell

  3. 3

    Avoid strong reference cycle in Block, can I simply do this

  4. 4

    Nested callback strong reference cycle

  5. 5

    Android: Strong reference cycle using ArrayAdapter

  6. 6

    Is this a strong reference cycle or memory leak at all?

  7. 7

    Strong reference cycle: closures vs methods

  8. 8

    Android: Strong reference cycle using ArrayAdapter

  9. 9

    How to break strong reference cycles in TypeScript?

  10. 10

    Swift class: reference cycle

  11. 11

    How to break a direct reference cycle in CPython

  12. 12

    How to break a direct reference cycle in CPython

  13. 13

    Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

  14. 14

    How to keep a strong reference inside of a block without having a retain cycle

  15. 15

    OK to use underscore for autosynthesized properties in block (strong reference cycle)

  16. 16

    How to keep a strong reference inside of a block without having a retain cycle

  17. 17

    Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

  18. 18

    Does [self new] in a block creates strong reference cycle?

  19. 19

    Swift SpriteKit, Can't fire bullet more than once

  20. 20

    How to break reference cycle between view controller and data source

  21. 21

    How to break reference cycle between view controller and data source

  22. 22

    How to reference UILabel strong reference programmatically using Swift?

  23. 23

    Swift + Storyboards: How to keep strong reference to show segue controller?

  24. 24

    Why Swift does not dispose circular references (strong reference cycles) automatically

  25. 25

    convert the reference back to a strong one inside the closure, memory management, swift

  26. 26

    Can't get retain cycle

  27. 27

    Can't reference Xcode GameScene.sks with swift

  28. 28

    Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

  29. 29

    Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

HotTag

Archive