Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCNAction.playAudio causes crash when nodes get deinitialized

We pinpointed the cause of the crash to the statement of using SCNAction.playAudio in our game. If any playAudio statements get called in our game, the deinitialization of the SCNScene/SCNView would later randomly trigger this crash:

enter image description here

How we play the audio:

func playAudioSource(from node: SCNNode, source audioSource: SCNAudioSource) {
    node.runAction(SCNAction.playAudio(audioSource, waitForCompletion: false))
}

It shows the EXC_BAD_ACCESS being at CPP3DAudioEngine::RemoveContext. We're developing for iOS 10.3 using SceneKit and Swift 3.

like image 870
mauris Avatar asked Apr 13 '17 14:04

mauris


1 Answers

You should provide more code to better understand what happen in your game but surely you can correct your function with:

func playAudioSource(from node: SCNNode, source audioSource: SCNAudioSource) {
    if let _ = node.parent, node.action(forKey: "playAudio") == nil {
        node.runAction(SCNAction.playAudio(audioSource, waitForCompletion: false),forKey:"playAudio")
    }
}

This prevents the launch of the action when the action it's already launched or in execution and check also if your node is already attached to it's parent (this can be useful, it depend from where you launch this code..)

like image 166
Alessandro Ornano Avatar answered Oct 23 '22 18:10

Alessandro Ornano