Global Steinberg::gPluginFactory variable

SDK for VST 3 audio plug-in and host development.
Post Reply
User avatar
pongasoft
Posts: 98
Joined: Sun Mar 11, 2018 5:57 pm
Location: Las Vegas, USA
Contact:

Global Steinberg::gPluginFactory variable

Post by pongasoft » Fri Apr 17, 2020 5:01 pm

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

Code: Select all

if(!gPluginFactory)
{
  //...
  gPluginFactory = new CPluginFactory(factoryInfo);
  //...
}
else
{
 else
   gPluginFactory->addRef()
}
Since the "main" API to the plugin is

Code: Select all

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

Arne Scheffler
Posts: 343
Joined: Mon Jun 20, 2016 7:53 am

Re: Global Steinberg::gPluginFactory variable

Post by Arne Scheffler » Tue Apr 21, 2020 2:05 pm

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

User avatar
pongasoft
Posts: 98
Joined: Sun Mar 11, 2018 5:57 pm
Location: Las Vegas, USA
Contact:

Re: Global Steinberg::gPluginFactory variable

Post by pongasoft » Thu Apr 23, 2020 4:23 pm

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:

Code: Select all

{
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

Arne Scheffler
Posts: 343
Joined: Mon Jun 20, 2016 7:53 am

Re: Global Steinberg::gPluginFactory variable

Post by Arne Scheffler » Fri Apr 24, 2020 6:30 am

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

Cheers,
Arne

Post Reply