createPluginInstance

In Juce 5.3.2 I used this code to create a wrapperprocessor that loads a VST2 plugin:

class WrapperProcessor
: public juce::AudioProcessor
{
public:
WrapperProcessor(juce::AudioPluginInstance* processorToUse): AudioProcessor(getBusesPropertiesFromProcessor(processorToUse)),plugin(processorToUse)

auto instance = new WrapperProcessor(pluginFormatManager.createPluginInstance(*pluginDescriptions[0], 44100.0, 512, msg));

This code does not longer work in Juce 5.4. It returns an error:

|Error|C2664|WrapperProcessor::WrapperProcessor(juce::AudioPluginInstance *) : Coversation of Argument 1 from std::unique_ptr<juce::AudioPluginInstance,std::default_deletejuce::AudioPluginInstance> to juce::AudioPluginInstance * not possible

What is the correct way of doing this in juce 5.4 and 6.1?

The declaration of AudioPluginFormatManager::createPluginInstance looks like this:

    std::unique_ptr<AudioPluginInstance> createPluginInstance (const PluginDescription& description,
                                                               double initialSampleRate, int initialBufferSize,
                                                               String& errorMessage) const;

It now returns a std::unique_ptr<AudioPluginInstance>. This interface makes it clearer that the resutling plugin instance is intended to be owned by the caller. You should adjust the constructor of WrapperProcessor to take a std::unique_ptr<AudioPluginInstance> argument:

class WrapperProcessor : public juce::AudioProcessor
{
public:
    WrapperProcessor (std::unique_ptr<juce::AudioPluginInstance> processorToUse)
        : AudioProcessor (getBusesPropertiesFromProcessor (*processorToUse)),
          plugin (std::move (processorToUse))
    {
    }

    // etc.

private:
    std::unique_ptr<juce::AudioPluginInstance> plugin;
};

I’d strongly advise against using a “raw” new call. Instead, prefer to use types that automatically manage resources, such as smart pointers.

auto wrapped = pluginFormatManager.createPluginInstance (*pluginDescriptions[0], 44100.0, 512, msg);
auto instance = std::make_unique<WrapperProcessor> (std::move (wrapped));

Thank you for your reply.
Unfortunately your proposal doesn’t seem to work.

class WrapperProcessor : public juce::AudioProcessor
{
public:
WrapperProcessor (std::unique_ptrjuce::AudioPluginInstance processorToUse)
: AudioProcessor (getBusesPropertiesFromProcessor (*processorToUse)),
plugin (std::move (processorToUse))
{
}

It returns an error:

E0413|No function for converting from juce::AudioPluginInstance to juce::AudioProcessor * found

The compiler can’t handle (*processorToUse)

I assume that getBusesPropertiesFromProcessor is a function you’ve written. You could either update its signature to take a const AudioProcessor& instead of an AudioProcessor*, or you could pass processorToUse.get() instead of *processorToUse.

Thanks. processorToUse.get() did solve the issue.

I tried the code you suggested:

juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{

auto wrapped = pluginFormatManager.createPluginInstance(*pluginDescriptions[0], 44100.0, 512, msg);
auto instance = std::make_unique(std::move(wrapped));
return instance.get();//is this correct??
}

It compiles. However I get a an error here:

AudioProcessor* JUCE_API JUCE_CALLTYPE createPluginFilterOfType (AudioProcessor::WrapperType type)
{
AudioProcessor::setTypeOfNextNewPlugin (type);
AudioProcessor* const pluginInstance = createPluginFilter();
AudioProcessor::setTypeOfNextNewPlugin (AudioProcessor::wrapperType_Undefined);

// your createPluginFilter() method must return an object!
jassert (pluginInstance != nullptr && pluginInstance->wrapperType == type);

It works when I use this code:

juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{ …
auto instance = new WrapperProcessor(std::move(pluginFormatManager.createPluginInstance(*pluginDescriptions[0], 44100.0, 512, msg)));
return instance;
}

Now I am confused if this is the correct way of doing it?

I didn’t realise that the new was inside createPluginFilter - this is one of the situations where it’s sort of fine (as long as you return the newed object immediately!). You could also release the unique_ptr after creating it.

juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
    auto wrapped = pluginFormatManager.createPluginInstance (*pluginDescriptions[0], 44100.0, 512, msg);
    return std::make_unique<WrapperProcessor> (std::move (wrapped)).release();
    // or
    // return new WrapperProcessor (std::move (wrapped));
}