Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:32:17+00:00 2026-06-05T05:32:17+00:00

I’m currently creating a sound system for my project. Every call PlayAsync creating instance

  • 0

I’m currently creating a sound system for my project. Every call PlayAsync creating instance of sound in std::thread callback. The sound data proceed in cycle in this callback. When thread proceeds it store sound instance in static vector. When thread ends (sound complete) – it delete sound instance and decrement instance count. When application ends – it must stop all sounds immediate, sending interrupt to every cycle of sound.

The problem is in array keeping these sounds. I am not sure, but I think vector isn’t right choice for this purpose.. Here is a code.

void gSound::PlayAsync()
{
    std::thread t(gSound::Play,mp_Audio,std::ref(*this));
    t.detach();
}
    HRESULT gSound::Play(IXAudio2* s_XAudio,gSound& sound)
{                
    gSound* pSound = new gSound(sound);   
    pSound->m_Disposed = false;         

    HRESULT hr;      
    // Create the source voice
    IXAudio2SourceVoice* pSourceVoice;
    if( FAILED( hr = s_XAudio->CreateSourceVoice( &pSourceVoice, pSound->pwfx ) ) )
    {
        gDebug::ShowMessage(L"Error creating source voice");      
        return hr;
    }

    // Submit the wave sample data using an XAUDIO2_BUFFER structure
    XAUDIO2_BUFFER buffer = {0};
    buffer.pAudioData = pSound->pbWaveData;
    buffer.Flags = XAUDIO2_END_OF_STREAM;  // tell the source voice not to expect any data after this buffer
    buffer.AudioBytes = pSound->cbWaveSize;

    if( FAILED( hr = pSourceVoice->SubmitSourceBuffer( &buffer ) ) )
    {
        gDebug::ShowMessage(L"Error submitting source buffer");
        pSourceVoice->DestroyVoice();   
        return hr;
    }

    hr = pSourceVoice->Start( 0 );     

    // Let the sound play
    BOOL isRunning = TRUE;
    m_soundInstanceCount++;     
    mp_SoundInstances.push_back(pSound); #MARK2
    while( SUCCEEDED( hr ) && isRunning && pSourceVoice != nullptr && !pSound->m_Interrupted)
    {    
        XAUDIO2_VOICE_STATE state;
        pSourceVoice->GetState( &state );
        isRunning = ( state.BuffersQueued > 0 ) != 0;     
        Sleep(10);
    }   
    pSourceVoice->DestroyVoice();   
    delete pSound;pSound = nullptr; //its correct ??
    m_soundInstanceCount--; 
    return 0;    
}

void gSound::InterrupAllSoundInstances()
{
    for(auto Iter = mp_SoundInstances.begin(); Iter != mp_SoundInstances.end(); Iter++)
    {
        if(*Iter != nullptr)//#MARK1
        {
        (*Iter)->m_Interrupted = true;
        }
    }
}

And this I call in application class before disposing sound objects, after main application loop immediate.

    gSound::InterrupAllSoundInstances();
while (gSound::m_soundInstanceCount>0)//waiting for deleting all sound instances in threads
{

}

Questions:

So #MARK1 – How to check memory validation in vector? I don’t have experience about it. And get errors when try check invalid memory (it’s not equals null)

And #MARK2 – How to use vector correctly? Or maybe vector is bad choice? Every time I create sound instance it increases size. It’s not good.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:32:18+00:00Added an answer on June 5, 2026 at 5:32 am

    A typical issue:

    delete pSound;
    pSound = nullptr; // issue
    

    This does not do what you think.

    It will effectively set pSound to null, but there are other copies of the same pointer too (at least one in the vector) which do not get nullified. This is why you do not find nullptr in your vector.

    Instead you could register the index into the vector and nullify that: mp_SoundInstances[index] = nullptr;.

    However, I am afraid that you simply do not understand memory handling well and you lack structure. For memory handling, it’s hard to tell without details and your system seems complicated enough that I am afraid it would tell too long to explain. For structure, you should read a bit about the Observer pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.