diff --git a/source/filters/filter-transform.cpp b/source/filters/filter-transform.cpp index bb49143a..d326d7c6 100644 --- a/source/filters/filter-transform.cpp +++ b/source/filters/filter-transform.cpp @@ -810,26 +810,27 @@ bool transform_factory::on_manual_open(obs_properties_t* props, obs_property_t* } #endif -std::shared_ptr _filter_transform_factory_instance = nullptr; - -void transform_factory::initialize() +std::shared_ptr transform_factory::instance() { - try { - if (!_filter_transform_factory_instance) - _filter_transform_factory_instance = std::make_shared(); - } catch (const std::exception& ex) { - D_LOG_ERROR("Failed to initialize due to error: %s", ex.what()); - } catch (...) { - D_LOG_ERROR("Failed to initialize due to unknown error.", ""); + static std::weak_ptr winst; + static std::mutex mtx; + + std::unique_lock lock(mtx); + auto instance = winst.lock(); + if (!instance) { + instance = std::shared_ptr(new transform_factory()); + winst = instance; } + return instance; } -void transform_factory::finalize() -{ - _filter_transform_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr transform_factory::get() -{ - return _filter_transform_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = transform_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-transform.hpp b/source/filters/filter-transform.hpp index a994cc54..2497ec13 100644 --- a/source/filters/filter-transform.hpp +++ b/source/filters/filter-transform.hpp @@ -96,10 +96,6 @@ namespace streamfx::filter::transform { #endif public: // Singleton - static void initialize(); - - static void finalize(); - - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::filter::transform diff --git a/source/plugin.cpp b/source/plugin.cpp index ca6d3a91..a08969c3 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -34,9 +34,6 @@ #ifdef ENABLE_FILTER_SDF_EFFECTS #include "filters/filter-sdf-effects.hpp" #endif -#ifdef ENABLE_FILTER_TRANSFORM -#include "filters/filter-transform.hpp" -#endif #ifdef ENABLE_FILTER_UPSCALING #include "filters/filter-upscaling.hpp" #endif @@ -165,9 +162,6 @@ MODULE_EXPORT bool obs_module_load(void) #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::initialize(); #endif -#ifdef ENABLE_FILTER_TRANSFORM - streamfx::filter::transform::transform_factory::initialize(); -#endif #ifdef ENABLE_FILTER_UPSCALING streamfx::filter::upscaling::upscaling_factory::initialize(); #endif @@ -226,9 +220,6 @@ MODULE_EXPORT void obs_module_unload(void) #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::finalize(); #endif -#ifdef ENABLE_FILTER_TRANSFORM - streamfx::filter::transform::transform_factory::finalize(); -#endif #ifdef ENABLE_FILTER_UPSCALING streamfx::filter::upscaling::upscaling_factory::finalize(); #endif