Removing SCNNode does not free memory before creating a new SCNNode

I am creating an application that uses Scenekit to display a scene based on the information returned from the database. I created a special class with a func class that creates an SCNNode containing everything you need to draw and returns it to my SCNView. Before calling this function, I delete any existing nodes. Everything works fine until I call him a second time. After deleting the old SCNNode, the memory is not freed before creating a new one. Is there a standard way to remove and replace SCNNode without memory overload? I am new to iOS developer and have never done graphics before. Thanks

+4
source share
1 answer

I had the same problem, there was a lot of memory with my SceneKit application. The memory is SCNNodefreed if you set the geometryvalue of your property nilbefore allowing Swift to de-initialize it.

Here is an example of how you can implement it:

class ViewController: UIViewController {
    @IBOutlet weak var sceneView: SCNView!
    var scene: SCNScene!

    // ...

    override func viewDidLoad() {
        super.viewDidLoad()
        scene = SCNScene()
        sceneView.scene = scene

        // ...
    }

    deinit {
        scene.rootNode.cleanup()
    }

    // ...
}

extension SCNNode {
    func cleanup() {
        for child in childNodes {
            child.cleanup()
        }
        geometry = nil
    }
}
0
source

Source: https://habr.com/ru/post/1610619/


All Articles