Global Steinberg::gPluginFactory variable

The code that creates the main factory (via BEGIN_FACTORY_DEF macro) does something like:

if(!gPluginFactory)
{
  //...
  gPluginFactory = new CPluginFactory(factoryInfo);
  //...
}
else
{
 else
   gPluginFactory->addRef()
}

Since the “main” API to the plugin is

Steinberg::IPluginFactory* PLUGIN_API GetPluginFactory ();

it seems to me that it should be the responsibility of the caller (DAW), to properly manage the lifecycle of the returned IPluginFactory. I am not sure I fully understand the use of that global variable gPluginFactory in the implementation besides not creating a factory a second time if already called.

I removed this usage entirely in my code (meaning I create a new factory every time it is called) and it works great (even better in my opinion as every time an instance of a plugin gets created by the DAW (at least with the VST2 wrapper), you get a new instance of the factory which is way cleaner than sharing across instances of a plugin!)

Is it fair to assume that DAWs are not relying on the existence of this gPluginFactory global variable to do their job (not sure how they could since it is not part of the API)? Are you aware of any code that relies on this variable being properly populated?

Thanks
Yan

Hi,
the global instance is an implementation detail. You’re free to not use the same technique. Some factories which can report a variable number of instances may be slow to generate if you do this whenever someone asks for the factory.

Cheers,
Arne

Thank you for your answer.

I have a follow up question in regards to the lifecycle of the factory. Looking at the api only (not the implementation provided) and the documentation, is it fair to assume that there is no guarantee that a host will keep the factory around as long as the plugin is alive?

For example, a host could do:

{
IPtr<IPluginFactory> factory = owned(GetFactory());

// use the factory to create processor and controller

factory->createInstance(...);

// done => factory goes out of scope => factory is destroyed
}

Am I reading the api/documentation properly or is a host required to keep the factory around as long as the processor and controller are alive?

Thanks for the clarification

You cannot make any assumption if a host will create only one factory or more.

Cheers,
Arne

I have some follow up questions on the usage of the factory.

  1. Can a host/daw create a factory when it starts (for example it loads all possible plugins) and whenever an instance of the plugin is needed it uses this factory? Or must a host/daw call GetInstanceFactory() for each plugin it creates?

  2. Can a host call

// pseudo code
GetInstanceFactory().create processor
GetInstanceFactory().create controller

or

factory = GetInstanceFactory()
factory.create processor
factory.create controller

Are both valid?

Thanks
Yan

  1. a host can cache the plugin factory when it starts
  2. both are valid

This confirms what I was thinking.

Thank you

Yan