Jump to content
  • Advertisement
Sign in to follow this  
Endurion

XAudio2 in dll, crash in FreeLibrary

This topic is 737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm currently adding a XAudio2 sound dll for my dll based engine and I'm stumbling on a seemingly simple task.

FreeLibrary always crashes at a random address when releasing the plugin dll. All I do is call XAudio2Create and CreateMasteringVoice. Before calling FreeLibrary I call the interface's Release method which stops all playing and shuts down the XAudio2 object properly. Interestingly the crash is gone if CreateMasteringVoice is not called.

From what the parallel stack window shows there seems to be a AudioSel.dll (?) thread still running. It looks like some sort of race condition. My educated guess: I'm shutting everything down but a worker thread is still running. When unloading obviously somebody get's his binary rug pulled from under its behind.

The problem also does not appear if I include the class instance directly (no DLL). The debug output shows no errors/warnings.

The object in the Dll is created on DLL_PROCESS_ATTACH (as a local static). The Release call cleans up all interfaces so there's nothing left but the object destructor.

If I run the same code in Release mode it still crashes, the entry in Event Viewer shows xaudio2_9.DLL as culprit. I'm on Windows 10 64 bit.


The init code:
HRESULT hr;
    GR::u32   flags = 0;
#ifdef DEBUG
    flags |= XAUDIO2_DEBUG_ENGINE;
#endif
    if ( FAILED( hr = XAudio2Create( &m_pX2Audio, flags, XAUDIO2_DEFAULT_PROCESSOR ) ) )
    {
      m_pDebugger->Log( "Sound", "XAudio2Create failed with %x", hr );
      return false;
    }

    if ( Channels <= 0 )
    {
      Channels = 16;
    }

    if ( FAILED( hr = m_pX2Audio->CreateMasteringVoice( &m_pMasteringVoice, Channels ) ) )
    {
      m_pDebugger->Log( "Sound", "CreateMasteringVoice failed with %x", hr );
      Release();
      return false;
    }
The release code:
    if ( m_pX2Audio != NULL )
    {
      m_pX2Audio->StopEngine();
    }
    for each ( auto& sound in m_Sounds )
    {
      if ( sound.second.pSourceVoice != NULL )
      {
        sound.second.pSourceVoice->Stop();
        sound.second.pSourceVoice->DestroyVoice();
      }
    }
    m_Sounds.clear();
    if ( m_pMasteringVoice != NULL )
    {
      m_pMasteringVoice->DestroyVoice();
      m_pMasteringVoice = NULL;
    }
    if ( m_pX2Audio != NULL )
    {
      m_pX2Audio->Release();
      m_pX2Audio = NULL;
    }
The DllMain:
    case DLL_PROCESS_ATTACH:
      Xtreme::XAudio2Sound::Instance();
      break;
    case DLL_PROCESS_DETACH:
      Xtreme::XAudio2Sound::Instance().Release();
      break;
Anybody encountered this problem and maybe found a workaround? Or see a potential cause in my code? Edited by Endurion

Share this post


Link to post
Share on other sites
Advertisement
Yeah, I stumbled upon that link too. The fix seems to target XAudio 2.7 only; as it seems, I'm getting XAudio 2.9 on Windows 10. The result is remarkably similar.

Duh! I still tried the workaround, and I had to replace 2.7 with 2.9 (and no debug lib), and it actually works! I can now switch sound plugins on the fly between DXSound8 and XAudio2. Yay :)


Still, it leaves an uneasy feeling. Before I linked to XAudio2.lib with no direct Dll dependency. As far as I understood, the runtime will choose the proper dll depending on OS. Now I basically fixed it to 2.9. Will that work on Win 7 or 8.x? Or will I have to check the OS version (urgh! red flag!) and load the corresponding DLL?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!