Fix crash on load: Add try/catch around findAllTypesForFile + createPluginInstance, use async path
Browse files1 unmodified line234567810 unmodified lines192021222324252 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838475 unmodified lines1601611621631641651662 unmodified lines1691701711721731741751 unmodified lineHostedPluginManager::HostedPluginManager(){// Register VST3 and AU formats explicitly (addDefaultFormats() removed in JUCE 8)#if JUCE_PLUGINHOST_VST3formatManager.addFormat(new juce::VST3PluginFormat());#endif10 unmodified linesvoid HostedPluginManager::loadPlugin(const juce::File& pluginFile,double sampleRate, int blockSize){// Unload existing plugin firstunloadPlugin();currentSampleRate = sampleRate;2 unmodified linesjuce::String errorMessage;juce::OwnedArray<juce::PluginDescription> typesFound;// Scan the file to find plugin descriptionsfor (int i = 0; i < formatManager.getNumFormats(); ++i){auto* format = formatManager.getFormat(i);format->findAllTypesForFile(typesFound, pluginFile.getFullPathName());if (typesFound.size() > 0)break;}if (typesFound.isEmpty()){DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName());return;}// Use the first plugin description foundauto& desc = *typesFound[0];// Create plugin instance (synchronous — safe here since we're on message thread)auto instance = formatManager.createPluginInstance(desc, sampleRate, blockSize, errorMessage);if (instance == nullptr){DBG("PluginBridge: Failed to create plugin: " + errorMessage);return;}// Enable all buses before useinstance->enableAllBuses();// Match bus layout: stereo in/outauto layout = juce::AudioProcessor::BusesLayout();layout.inputBuses.add(juce::AudioChannelSet::stereo());layout.outputBuses.add(juce::AudioChannelSet::stereo());if (instance->checkBusesLayoutSupported(layout))instance->setBusesLayout(layout);// Prepare for playbackinstance->prepareToPlay(sampleRate, blockSize);// Store{const juce::ScopedLock sl(pluginLock);hostedPlugin = std::move(instance);loadedPluginPath = pluginFile.getFullPathName();}DBG("PluginBridge: Loaded plugin: " + getPluginName()+ " (" + juce::String(getParameterCount()) + " params)");}void HostedPluginManager::unloadPlugin()75 unmodified linesinfo.displayText = param->getText(param->getValue(), 64);info.automatable = param->isAutomatable();// Try to get stable ID from HostedParameterif (auto* hosted = dynamic_cast<juce::AudioPluginInstance::HostedParameter*>(param))info.id = hosted->getParameterID();else2 unmodified linesresults.push_back(info);}// Cap results to avoid token bloatif (results.size() >= 20)break;}1 unmodified line23456710 unmodified lines1819202122232 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909175 unmodified lines1671681691701711722 unmodified lines1751761771781791801 unmodified lineHostedPluginManager::HostedPluginManager(){#if JUCE_PLUGINHOST_VST3formatManager.addFormat(new juce::VST3PluginFormat());#endif10 unmodified linesvoid HostedPluginManager::loadPlugin(const juce::File& pluginFile,double sampleRate, int blockSize){unloadPlugin();currentSampleRate = sampleRate;2 unmodified linesjuce::String errorMessage;juce::OwnedArray<juce::PluginDescription> typesFound;// Wrap in try/catch — some plugins throw or show dialogs that cause exceptionstry{for (int i = 0; i < formatManager.getNumFormats(); ++i){auto* format = formatManager.getFormat(i);format->findAllTypesForFile(typesFound, pluginFile.getFullPathName());if (typesFound.size() > 0)break;}}catch (...){DBG("PluginBridge: Exception during plugin scan for: " + pluginFile.getFullPathName());return;}if (typesFound.isEmpty()){DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName());return;}auto desc = *typesFound[0];// Use async creation — safer for AU plugins and plugins that need message threadformatManager.createPluginInstanceAsync(desc, sampleRate, blockSize,[this, pluginFile](std::unique_ptr<juce::AudioPluginInstance> instance,const juce::String& error){if (instance == nullptr){DBG("PluginBridge: Failed to create plugin: " + error);return;}// Enable all busesinstance->enableAllBuses();// Match bus layout: stereo in/outauto layout = juce::AudioProcessor::BusesLayout();layout.inputBuses.add(juce::AudioChannelSet::stereo());layout.outputBuses.add(juce::AudioChannelSet::stereo());if (instance->checkBusesLayoutSupported(layout))instance->setBusesLayout(layout);// Prepare for playbackinstance->prepareToPlay(currentSampleRate, currentBlockSize);// Store{const juce::ScopedLock sl(pluginLock);hostedPlugin = std::move(instance);loadedPluginPath = pluginFile.getFullPathName();}DBG("PluginBridge: Loaded plugin: " + getPluginName()+ " (" + juce::String(getParameterCount()) + " params)");});}void HostedPluginManager::unloadPlugin()75 unmodified linesinfo.displayText = param->getText(param->getValue(), 64);info.automatable = param->isAutomatable();if (auto* hosted = dynamic_cast<juce::AudioPluginInstance::HostedParameter*>(param))info.id = hosted->getParameterID();else2 unmodified linesresults.push_back(info);}if (results.size() >= 20)break;}
Source/HostedPluginManager.cpp+49-44