How to Safely Load a .VST3 while using an AudioAppComponent

Would anyone happen to know the proper procedure to load a plugin while getNextAudioBlock is running?

void loadPluginChannel(int file_index, int track)
{

   // shutdownAudio();

    if (plugins[track])
    {

        basicWindow[track]->removeFromDesktop();

        plugins[track]->releaseResources();

        plugins[track].reset();

    }
   

    track_slected = track;

    pluginmanager.addDefaultFormats();


    descs.clear();

    // the single .vst3 file may have multiple plugin types, so we need to deal with an array
    // of PluginDescriptions
    juce::VST3PluginFormat v3format;
    v3format.findAllTypesForFile(descs, plugin_file[file_index]);
    if (descs.size() > 0)
    {
      

        plugins[track] = pluginmanager.createPluginInstance(*descs[0], sampleRateHost, samplesPerBlock, error);






        if (!plugins[track])
        {

            //plugins.insert(0, (pluginmanager.createPluginInstance(*descs[0], 44100, 512, error)));
            //std::cout << error << "\n";
        }
        else
        {
           error_message = descs[0]->descriptiveName;


           numOutputChannels = plugins[track]->getNumOutputChannels();

           plugins[track]->enableAllBuses();
           

           audioprocessoreditor = plugins[track]->createEditor();

          
           

           bounds = audioprocessoreditor->getBounds();

           basicWindow[track] = new BasicWindow(descs[0]->descriptiveName, juce::Colours::black, 7, true);

           //basicWindow->setBounds(720, 280, 600, 400);

           basicWindow[track]->centreWithSize(bounds.getWidth(), bounds.getHeight());

           basicWindow[track]->setContentComponent(audioprocessoreditor, false);

           basicWindow[track]->setVisible(true);

           plugins[track]->prepareToPlay(sampleRateHost, samplesPerBlock);

           


        }

    }


   // setAudioChannels(2, 2);



}

This Code works but its crashing when I try to load another plugin over the top.

Any help would be appreciated.

Well no one could answer this but, if you load the instance with

           plugins[track] = pluginmanager.createPluginInstance(*descs[0], sampleRateHost, samplesPerBlock, error);

directly you don’t lose the instance. I’m no expert if someone has any information please.

The question is a bit vague, so it’s difficult to suggest a good answer.

If you’re attempting to replace the plugin instance while it is simultaneously processing audio, that will cause problems because the plugin may be deleted while its data members are still being used. You’ll probably need to use some kind of wait-free synchronisation mechanism like a queue or a spinlock in order to make sure that you don’t attempt to destroy the plugin on the main thread while it’s still in use on the audio thread.