VST Gets Created Twice in Host, Breaks VST


#1

Hello,

I’m trying to load a VST inside a JUCE plugin host and am experiencing some strange behavior only in my Windows build (no issues with OSX). The plugin is a sampler with internal plugin effect modules that are instantiated in a singleton registry. With JUCE 4.3 and up, when I load this vst, the effects modules disappear.

Now, I’ve traced the issue to the function in juce_VSTPluginFormat.cpp:
static VSTPluginInstance* create (const ModuleHandle::Ptr& newModule, double initialSampleRate, int initialBlockSize)

This function seems to be creating the vst twice and, probably due to a bug in the vst, the effects module instance registry gets corrupted on the second loading. I think there’s an issue with releasing the first instance before creating the second one.

Though I’m trying to find the root of the cause in the vst, commenting out the code that loads the vst the second time around seems to fix the problem. So, my actual question is: why does the vst need to be created twice, and are there any consequences to altering the function as follows?

static VSTPluginInstance* create (const ModuleHandle::Ptr& newModule,
                                  double initialSampleRate,
                                  int initialBlockSize)
{
    if (VstEffectInterface* newEffect = constructEffect (newModule))
    {
        /* commented out
        newEffect->hostSpace2 = 0;

        newEffect->dispatchFunction (newEffect, plugInOpcodeIdentify, 0, 0, 0, 0);

        newEffect->dispatchFunction (newEffect, plugInOpcodeSetSampleRate, 0, 0, 0, static_cast<float> (initialSampleRate));
        newEffect->dispatchFunction (newEffect, plugInOpcodeSetBlockSize,  0, jmax (32, initialBlockSize), 0, 0);

        newEffect->dispatchFunction (newEffect, plugInOpcodeOpen, 0, 0, 0, 0);
         */
        
        BusesProperties ioConfig = queryBusIO (newEffect);
        
        /* commented out
        newEffect->dispatchFunction (newEffect, plugInOpcodeClose, 0, 0, 0, 0);

        newEffect = constructEffect (newModule);
         */

        if (newEffect != nullptr)
            return new VSTPluginInstance (newModule, ioConfig, newEffect);
    }

    return nullptr;
}

#2

Hi. I’m not sure why it gets created/destroyed/created (maybe the JUCE devs can comment), but there’s usually a problem in your code (from my experience) if this cycle causes an issue. Ultimately, if this causes a problem in the plugin host it’s going to cause a problem in a real host if people are creating/deleting plugin instances - maybe this is why the plugin host does it, as a test?


#3

I think that if you remove all the registered functions, you won’t get the interactions you are looking for in your plugin. I suppose that the first call is there to check that you can instantiate the module, and the second call is made to set it up properly. Why isn’t it done in the same step? Probably because you need to have a common interface to all the different flavors of plugins, and this was the easiest and most maintainable way to do it.

If your plugin can’t be created twice, you have a serious bug in your effect.


#4

Thanks guys.

Looks like they just quietly fixed this for VST2 hosting after I posted:

https://github.com/WeAreROLI/JUCE/commit/4c03a9079b4e1ca668c686ad3c378b57960ddc79

Though I’m still a little confused on whether there is some fatal flaw in my vst code or if this was just a host bug. Couldn’t find any issues on my side. I guess I’ll move on and hope for the best.


#5

interesting… i think I might add it back to my build tho - as I say, it was a good sanity check :slight_smile:


#6

Hmmmm, git blame definitely says that I’m the culprit: it doesn’t really look like a typo to me so there must have been some reason why I put it there but searching the forum (and my memory) didn’t really help me refresh my memory.

As you’ve noticed already, I’ve removed the double instantiation and tested it on the most popular plug-ins. Let’s see if anybody complains.

Note to myself: always add comments when adding workarounds :slight_smile:.