PluginProcessor constructor is called twice by audio plugin host


#1

My audio plugin [0] works… sometimes. The problem is that sometimes it gets initialised incorrectly.

I run it in JUCE’s “Plugin Host.app”. When I add my VST3 plugin to the host, I notice that my PluginProcessor’s constructor gets called twice. So my private initialiseSynth() gets invoked twice, and finds that it has already been initialised before. Sometimes it survives this fine, other times I end up with segfaults when I rely on the data that I’ve constructed.

Here’s the exact code of my PluginProcessor:

These are the two stack traces I captured — the two times that my constructor was invoked. It seems that juce::VST3PluginFormat::createPluginInstance() is the common ancestor. Once inside that function, it constructs my audio plugin twice. Is this normal?

[0] It’s a simple synthesizer that takes MIDI input, uses fluidsynth to render each block through a soundfont.


#2

It’s quite normal for hosts to scan a plugin on startup which involves loading it. Also keep in mind that your plugin could have any number of instances open at any one time so you need to make sure you can deal with that. Are you relying on static variables or some kind of singleton maybe?


#3

I don’t have any singletons or statics.* one suspect is the fluidsynth library I link against, but that doesn’t obviously rely on singletons/statics either (the entry point is to request that it construct an instance for you to use).

*Disclaimer: I am not a C++ expert


#5

The problem is, that you don’t initialise your initialised variable.

Either change this line to

bool initialised = false;

Or set it to false in the constructor.

In C++ all simple types need to be initialised manually, since the compiler assumes, it might not be needed.

Same goes for the other variables as well, don’t assume they have a certain value.


#6

wow, thanks for investigating! I’ll try this out. that would also explain why channel sometimes began its life set to a large random number…


#7

About C++ and initialisation, watch Scott Meyers at the d-conf (obviously). It is very insightful and entertaining as well, if in a hurry start at ca. 6:12 minutes: