0

In my GameViewController I have this code that initialises the game:

scnView = SCNView(frame: self.view.frame)
    scnView.backgroundColor = UIColor(red: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 1.0)
    scnView.showsStatistics = true
    scnView.antialiasingMode = SCNAntialiasingMode.Multisampling2X
    scnView.overlaySKScene = SKScene(size: self.view.bounds.size)
    scnView.playing = true
    self.view.addSubview(scnView)
    self.view.sendSubviewToBack(scnView)

    // Set up the scene
    let scene = GameScene(view: scnView, delegate: self)
    scene.rootNode.hidden = true
    scene.physicsWorld.contactDelegate = scene

    // start playing the scene
    scnView.scene = scene
    scnView.delegate = scene
    scnView.scene!.rootNode.hidden = false
    scnView.play(self)

It sets up the scene and the environment. When I call this the memory usage goes to 40MB and remains constant at that amount. When I end the game, I run this code:

self.scnView.removeFromSuperview()
self.scnView = nil
self.initializeGame()

I remove the view and re-start the game from scratch. However the memory allocation increases to 70MB and keeps increasing the more I do this. I tried moving my .dae file allocations in the GameControllerView so that the code was only called once:

static let HomeLifeguard_1 = SCNScene(named: String(format: "assets.scnassets/Models/HomeLifeguard_1.dae"))

I tried using deinit{} and setting most of my variables to nil, but nothing changes. I don't understand what is holding the memory. Shouldn't scnView = nil deallocate memory automatically?

Alessandro
  • 4,000
  • 12
  • 63
  • 131
  • Try attaching your session to Instrument's Allocations and see if you can isolate the memory leak. You may have a Strong Reference Cycle which can be resolved with a simple "weak" keyword somewhere. That's all I can offer unfortunately. Hopefully someone more knowledgeable comes along. GL – Dan Beaulieu Mar 08 '16 at 18:21
  • 2
    Is the view property of GameScene declared weak or unowned? Perhaps you have a strong reference cycle between the scene and scnView. scene appears to hold a reference to scnView, and scnView holds a reference to scene via the `scene` property, which according to the `SCNView` documentation is not declared weak or unowned. – Matthew Seaman Mar 09 '16 at 01:26
  • See http://stackoverflow.com/questions/30892837/in-swift-how-to-get-memory-back-to-normal-after-an-skscene-is-removed?rq=1 – Matthew Seaman Mar 09 '16 at 03:27

1 Answers1

2

I also meet the same case.And I find a solution to fix the bug.You must asynchronous to remove and release scnView.Just look the following code.

    _scnView.antialiasingMode = SCNAntialiasingModeNone;
    __block SCNView *strongScnView = _scnView;
    _scnView = nil;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [strongScnView setScene:nil];
            [strongScnView removeFromSuperview];
            [strongScnView stop:nil];
            strongScnView = nil;
        });
shaun.kang
  • 36
  • 3