From 3c4801bc969e10692570e11fbdfec0685ac7fead Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 2 Oct 2020 15:01:01 +0200 Subject: [PATCH] VST3: common factory instantiating and release This may address some issues with multiple references (and incremental releases) of a plugin's factory. --- libs/ardour/ardour/vst3_module.h | 15 +++++++++++++-- libs/ardour/ardour/vst3_plugin.h | 1 - libs/ardour/vst3_module.cc | 24 ++++++++++++++++++++++++ libs/ardour/vst3_plugin.cc | 18 ++++-------------- libs/ardour/vst3_scan.cc | 10 +--------- 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/libs/ardour/ardour/vst3_module.h b/libs/ardour/ardour/vst3_module.h index b88f58d250..579f24316b 100644 --- a/libs/ardour/ardour/vst3_module.h +++ b/libs/ardour/ardour/vst3_module.h @@ -23,23 +23,34 @@ #include "ardour/libardour_visibility.h" +namespace Steinberg { + class IPluginFactory; +} + namespace ARDOUR { class LIBARDOUR_API VST3PluginModule { public: static boost::shared_ptr load (std::string const& path); - VST3PluginModule () {} + + VST3PluginModule () : _factory (0) {} virtual ~VST3PluginModule () {} - virtual void* fn_ptr (const char* name) const = 0; + Steinberg::IPluginFactory* factory (); protected: + void release_factory (); + virtual bool init () = 0; virtual bool exit () = 0; + virtual void* fn_ptr (const char* name) const = 0; +private: /* prevent copy construction */ VST3PluginModule (VST3PluginModule const&); + + Steinberg::IPluginFactory* _factory; }; } // namespace ARDOUR diff --git a/libs/ardour/ardour/vst3_plugin.h b/libs/ardour/ardour/vst3_plugin.h index 60edebf923..11431eb06d 100644 --- a/libs/ardour/ardour/vst3_plugin.h +++ b/libs/ardour/ardour/vst3_plugin.h @@ -176,7 +176,6 @@ private: std::vector _connections; FUID _fuid; - IPluginFactory* _factory; Vst::IComponent* _component; Vst::IEditController* _controller; IPlugView* _view; diff --git a/libs/ardour/vst3_module.cc b/libs/ardour/vst3_module.cc index 584e776c39..dc3fb0fa02 100644 --- a/libs/ardour/vst3_module.cc +++ b/libs/ardour/vst3_module.cc @@ -31,6 +31,7 @@ #include "pbd/error.h" #include "pbd/failed_constructor.h" +#include "vst3/pluginterfaces/base/ipluginbase.h" #include "ardour/vst3_module.h" #include "pbd/i18n.h" @@ -72,6 +73,7 @@ public: ~VST3MacModule () { + release_factory (); if (_bundle) { exit (); CFRelease (_bundle); @@ -126,6 +128,7 @@ public: ~VST3WindowsModule () { + release_factory (); if (_handle) { exit (); FreeLibrary (_handle); @@ -186,6 +189,7 @@ public: ~VST3LinuxModule () { + release_factory (); if (_dll) { exit (); dlclose (_dll); @@ -217,6 +221,26 @@ private: #endif +Steinberg::IPluginFactory* +VST3PluginModule::factory () +{ + if (!_factory) { + GetFactoryProc fp = (GetFactoryProc)fn_ptr ("GetPluginFactory"); + if (fp) { + _factory = fp (); + } + } + return _factory; +} + +void +VST3PluginModule::release_factory () +{ + if (_factory) { + _factory->release (); + } +} + boost::shared_ptr VST3PluginModule::load (std::string const& path) { diff --git a/libs/ardour/vst3_plugin.cc b/libs/ardour/vst3_plugin.cc index 414a82419f..a1660a19f6 100644 --- a/libs/ardour/vst3_plugin.cc +++ b/libs/ardour/vst3_plugin.cc @@ -941,7 +941,6 @@ VST3PluginInfo::is_instrument () const VST3PI::VST3PI (boost::shared_ptr m, std::string unique_id) : _module (m) - , _factory (0) , _component (0) , _controller (0) , _view (0) @@ -954,13 +953,9 @@ VST3PI::VST3PI (boost::shared_ptr m, std::string uniqu , _n_factory_presets (0) { using namespace std; + IPluginFactory* factory = m->factory (); - GetFactoryProc fp = (GetFactoryProc)m->fn_ptr ("GetPluginFactory"); - if (!fp) { - throw failed_constructor (); - } - - if (!(_factory = fp ())) { + if (!factory) { throw failed_constructor (); } @@ -968,7 +963,7 @@ VST3PI::VST3PI (boost::shared_ptr m, std::string uniqu throw failed_constructor (); } - if (_factory->createInstance (_fuid.toTUID (), Vst::IComponent::iid, (void**)&_component) != kResultTrue) { + if (factory->createInstance (_fuid.toTUID (), Vst::IComponent::iid, (void**)&_component) != kResultTrue) { throw failed_constructor (); } @@ -980,7 +975,7 @@ VST3PI::VST3PI (boost::shared_ptr m, std::string uniqu if (!_controller) { TUID controllerCID; if (_component->getControllerClassId (controllerCID) == kResultTrue) { - if (_factory->createInstance (controllerCID, Vst::IEditController::iid, (void**)&_controller) != kResultTrue) { + if (factory->createInstance (controllerCID, Vst::IEditController::iid, (void**)&_controller) != kResultTrue) { throw failed_constructor (); } if (_controller && (_controller->initialize (HostApplication::getHostContext ()) != kResultOk)) { @@ -1137,13 +1132,8 @@ VST3PI::terminate () _component->release (); - if (_factory) { - _factory->release (); - } - _controller = 0; _component = 0; - _factory = 0; } bool diff --git a/libs/ardour/vst3_scan.cc b/libs/ardour/vst3_scan.cc index 5cb76be6c4..4360e9abeb 100644 --- a/libs/ardour/vst3_scan.cc +++ b/libs/ardour/vst3_scan.cc @@ -90,14 +90,7 @@ ARDOUR::discover_vst3 (boost::shared_ptr m, std::vectorfn_ptr ("GetPluginFactory"); - - if (!fp) { - cerr << "Failed to find 'GetPluginFactory' function\n"; - return false; - } - - IPluginFactory* factory = fp (); + IPluginFactory* factory = m->factory (); if (!factory) { cerr << "Failed to get VST3 plug-in factory\n"; @@ -199,7 +192,6 @@ ARDOUR::discover_vst3 (boost::shared_ptr m, std::vectorrelease (); return true; }