removing SCNNode does not free memory before creating new SCNNode

孤人 提交于 2019-12-19 08:11:49

问题


I am building an app that uses Scenekit to display a scene based on information returned from a data base. I have created a custom class with a class func that creates a SCNNode containing everything that needs to be drawn and returns it to my SCNView. Before calling this function I remove any existing nodes. Everything works great until I need to call it a second time. After removing the old SCNNode The memory is not deallocated before creating the new one. Is there a standard way of removing and replacing an SCNNode without overloading memory? I'm new to iOS dev and have never really done graphics before either. Thanks


回答1:


I had the same problem, with my SceneKit app leaking a lot of memory. The memory of the SCNNode is freed if you set its geometry property to nil before letting Swift deinitialize it.

Here is an example of how you may 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
    }
}


来源:https://stackoverflow.com/questions/32997711/removing-scnnode-does-not-free-memory-before-creating-new-scnnode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!