Read acces violations with OwnedArray and drawing editors from a audioprocessorgraph

Hi,

I have a plugin where I have different fx processors that you can select with a combobox, whenever you select a combobox everything works until I close the plugin. When I have changed the combobox once, everything works fine. But when I change the combobox to use a different processor as the first one I and the close the plugin I get:

Read acces violation in juce_win32_Threads.cpp on line 46:

void CriticalSection::enter() const noexcept {EnterCriticalSection ((CRITICAL_SECTION*) lock); }

When I change one of the comboxes to empty after selecting the first processor and then close the plugin I get

Read acces violation in juce_WeakReference.h on line 132:

inline ObjectType* get() const noexcept {return Owner;}

value: 0xddddddddd
Type: const juce::WeakReference<juce::Component,juce::ReferenceCountedObject::SharedPointer *

Code:

void ModuleBoxComponent::editorsChanged()
{
  removeAllChildren();

  for (int i = 0; i < 3; ++i)
  {
	  int choice = choices[i];
	  auto slot = audioProcessor.slots[i];
	  if (slot != nullptr)
	  {
		  if (choice == 1)            // [1]
		  {
			  editArray.insert(i, nullptr);
		  }
		  else
		  {
			  //if (slot->getProcessor() != nullptr) {
				  editArray.insert(i, std::unique_ptr<juce::AudioProcessorEditor>(slot->getProcessor()->createEditor()));

			  //}
		  }
	  }
	  else
	  {
		  editArray.insert(i, nullptr);
	  }
	}
  
  int startX = 0;
  for (int i = 0; i < 3; ++i)
  {	  
	  auto editor = editArray[i];
	  if(editor != nullptr)
	  {
		  addAndMakeVisible(*editor);
		  editor->setBounds(startX, 0, 100, 100);
		  
	  }
	  startX += 400;
  }
	
 }

editArray is a juce::OwnedArray <juce::AudioProcessorEditor> editArray;

in pluginprocessor.h :

void RookieBoxAudioProcessor::updateGraph()
{

  bool hasChanged = false;

  juce::Array<std::atomic<float>*> choices {parameters.getRawParameterValue("SLOT1"),
                                            parameters.getRawParameterValue("SLOT2"),
                                            parameters.getRawParameterValue("SLOT3") };

  juce::Array<juce::RangedAudioParameter*> bypasses {parameters.getParameter("BYPASS1"),
                                                     parameters.getParameter("BYPASS2"),
                                                     parameters.getParameter("BYPASS3")};

  slots.add (slot1Node);
  slots.add (slot2Node);
  slots.add (slot3Node);

 for (int i = 0; i < 3; ++i)
 {
     auto choice = choices[i]->load();
     auto  slot   = slots.getUnchecked (i);

     if (choice == 1)            // [1]
     {
         if (slot != nullptr)
         {
             mainProcessor->removeNode (slot.get());
             slots.set (i, nullptr);
             hasChanged = true;
         }

     }
     else if (choice == 2)       // [2]
     {
         if (slot != nullptr)
         {
             if (slot->getProcessor()->getName() == "Gain")
             {
               continue;
             }


             mainProcessor->removeNode (slot.get());
         }

         slots.set (i, mainProcessor->addNode (std::make_unique<GainProcessor>()));
         hasChanged = true;
     }

     else if (choice == 3)       // [2]
     {
         if (slot != nullptr)
         {
             if (slot->getProcessor()->getName() == "Flanger")
             {
               continue;
             }


             mainProcessor->removeNode (slot.get());
         }

         slots.set (i, mainProcessor->addNode (std::make_unique<FlangerProcessor>()));
         hasChanged = true;
     }

     else if (choice == 4)       // [2]
     {
         if (slot != nullptr)
         {
             if (slot->getProcessor()->getName() == "Delay")
             {
               continue;
             }


             mainProcessor->removeNode (slot.get());
         }

         slots.set (i, mainProcessor->addNode (std::make_unique<DelayProcessor>()));
         hasChanged = true;
     }

     else if (choice == 5)       // [2]
     {
         if (slot != nullptr)
         {
             if (slot->getProcessor()->getName() == "Distortion")
             {
               continue;
             }


             mainProcessor->removeNode (slot.get());
         }

         slots.set (i, mainProcessor->addNode (std::make_unique<DistortionProcessor>()));
         hasChanged = true;
     }
   }
//! [updateGraph loop]

//! [updateGraph connect]
 if (hasChanged)
 {
     for (auto connection : mainProcessor->getConnections())     // [5]
         mainProcessor->removeConnection (connection);

     juce::ReferenceCountedArray<Node> activeSlots;
     int j = 0;
     for (auto slot : slots)
     {
         if (slot != nullptr)
         {
            activeSlots.add (slot);                             // [6]

            slot->getProcessor()->setPlayConfigDetails (getMainBusNumInputChannels(),
                                                        getMainBusNumOutputChannels(),
                                                     getSampleRate(), getBlockSize());
			
         }
         else
         { 
         }
           ++j;
     }

     if (activeSlots.isEmpty())                                  // [7]
     {
         connectAudioNodes();
     }

     else
     {
         for (int i = 0; i < activeSlots.size() - 1; ++i)        // [8]
         {

             for (int channel = 0; channel < 2; ++channel)
                 mainProcessor->addConnection ({ { activeSlots.getUnchecked (i)->nodeID,      channel },
                                                 { activeSlots.getUnchecked (i + 1)->nodeID,  channel } });
         }

         for (int channel = 0; channel < 2; ++channel)           // [9]
         {
             mainProcessor->addConnection ({ { audioInputNode->nodeID,         channel },
                                             { activeSlots.getFirst()->nodeID, channel } });
             mainProcessor->addConnection ({ { activeSlots.getLast()->nodeID,  channel },
                                             { gainNode->nodeID,        channel } });
             mainProcessor->addConnection ({ { gainNode->nodeID,  channel },
                                             { audioOutputNode->nodeID,        channel } });
         }
     }


     for (auto node : mainProcessor->getNodes())                 // [10]
         node->getProcessor()->enableAllBuses();
 }

}