Dave
Dave

Reputation: 41

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; }
}

Upvotes: 0

Views: 900

Answers (2)

Chuck Walbourn
Chuck Walbourn

Reputation: 41077

A key thing to remember about XAudio2 is that a lot of the functionality is async, and you the app developer are responsible for keeping the source audio memory 'alive' until XAudio2 is done with it. As such, you have to be very careful about lifetimes and shutdown.

You might want to take a look at DirectX Tool Kit for Audio which has a basic audio manager and playback system for XAudio2.

The issue you are describing sounds a lot like this lifetime issue with XAudio 2.7: See this blog post for the details and workaround.

There's not a lot of value in support Windows Vista, but Windows 7 Service Pack 1 still requires using XAudio 2.7 and the legacy DirectX SDK and the legacy DirectSetup REDIST. If your minimum was Windows 8.x or Windows 10, then you could count on XAudio 2.8 or 2.9 being available as part of the OS. For the details here, see this blog post.

UPDATE There's now a way to use the latest XAudio 2.9 on Windows 7. See Microsoft Docs.

Upvotes: 2

Dave
Dave

Reputation: 41

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.

Upvotes: 1

Related Questions