mauris
mauris

Reputation: 43619

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.

Upvotes: 5

Views: 477

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35412

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..)

Upvotes: 5

Related Questions