Reputation: 4100
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?
Upvotes: 0
Views: 1165
Reputation: 36
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;
});
Upvotes: 2