Search code examples
c++directx-9xaudio2

DirectX SDK June 2010 : XAudio2 crashes on every app exit


I'm developping a C++ game engine based on Direct3D 9 and XAudio 2.7. And I have the same problem everytime I exit my test game : XAudio crashes. I'm sure it's this particular component that is causing the problem because when I remove the call to initialization, the game quits without any problems.

Exception box

I don't know why, I've set up all the base code (initialization, mastering voice, audio components, clear all buffers and shutdown with ->Release();), tried with and without XAudio 3D...

If you've already experienced that, you're welcome to help. Thanks.

I'm running Windows 10 Pro 64-bits

Code :

AudioEngineXA2::AudioEngineXA2()
{
    m_xaudio2 = 0;
    m_xa2MasteringVoice = 0;

    m_x3d = 0;
}

void AudioEngineXA2::initialize()
{
    if ( XAudio2Create( &m_xaudio2, 0 ) < 0 || m_xaudio2->CreateMasteringVoice( &m_xa2MasteringVoice ) < 0 )
    {
        cout << "XAudio2 initialization failed!" << endl;
        return;
    }

    //X3DAudioInitialize( SPEAKER_STEREO, X3DAUDIO_SPEED_OF_SOUND, (unsigned char*) m_x3d );

    // Uncommenting this part doesn't help either
    /*XAUDIO2_DEVICE_DETAILS* devdet = 0;
    m_xaudio2->GetDeviceDetails(0, devdet);

    float* matrix = new float[devdet->OutputFormat.Format.nChannels];
    DSPSettings.SrcChannelCount = 1;
    DSPSettings.DstChannelCount = devdet->OutputFormat.Format.nChannels;
    DSPSettings.pMatrixCoefficients = matrix;*/
}

void AudioEngineXA2::cleanup()
{
    m_xa2MasteringVoice->DestroyVoice();

    m_xaudio2->StopEngine();
    m_xaudio2->Release();
    //if ( m_x3d ) { delete[] m_x3d; m_x3d = 0; }
}

Solution

  • So I eventually took a look into the DirectX SDK samples and found out that XAudio2's initialization (and deinit) needed two other lines which were litterally mentioned in basically none of all the XAudio2 tutorials over the whole internet.

    You need to call "CoInitializeEx( 0, COINIT_MULTITHREADED );" before "XAudio2Create();" and "CoUninitialize();" after releasing XAudio2.