Search code examples
core-audioavaudioengine

AVAudioNode gets deallocated immediately


I can not create and attach AVAudioNode to AVAudioEngine as it gets deallocated immediately.

I've created a subclass of AVAudioNode, added deinit and during debug it was triggered immediately.

I've tried to log this in console and got <uninitialized> response.

po AVAudioNode()

On the contrary, it works with AVAudioMixerNode

po AVAudioMixerNode()

I'm not storing it weakly. I set it as a strong property in my class/actor.

class MyClass {
    let node = AVAudioNode()
    let engine = AVAudioEngine()
    
    fun foo() {
        print(node) // This will crash the app
        engine.attach(node) // As well as this
    }
}

Solution

  • AVAudioNode is defined as an interface.

    @interface AVAudioNode : NSObject {
    

    Interfaces are not concrete; you do not instantiate them. They are for inheritance purposes only.

    AVAudioMixerNode is a concrete class that implements the AVAudioNode interface. That is why you see it in the console when you po it.

    open class AVAudioMixerNode : AVAudioNode, AVAudioMixing {