From 219babcb7a46a4f99e7663b6e8119f0ecb79ebd4 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 11 Dec 2020 07:52:19 +0100 Subject: [PATCH] Avoid making the controller take a reference on the editor --- vst/SfizzVstController.cpp | 30 +++++++++++++++--------------- vst/SfizzVstController.h | 3 ++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/vst/SfizzVstController.cpp b/vst/SfizzVstController.cpp index 9e5bf897..9ac894c4 100644 --- a/vst/SfizzVstController.cpp +++ b/vst/SfizzVstController.cpp @@ -139,14 +139,14 @@ IPlugView* PLUGIN_API SfizzVstController::createView(FIDString _name) if (name != Vst::ViewType::kEditor) return nullptr; - if (_editor) { - withStateLock([this]() { - _uiState = _editor->getCurrentUiState(); + if (IPtr editor = _editor.lock()) { + withStateLock([this, editor]() { + _uiState = editor->getCurrentUiState(); }); } - SfizzVstEditor* editor = new SfizzVstEditor(this); - _editor = Steinberg::owned(editor); + IPtr editor = Steinberg::owned(new SfizzVstEditor(this)); + _editor = editor->getWeakPtr(); withStateLock([this, editor]() { editor->updateState(_state); @@ -209,14 +209,14 @@ tresult PLUGIN_API SfizzVstController::setParamNormalized(Vst::ParamID tag, Vst: if (slotF32 && *slotF32 != value) { withStateLock([this, slotF32, value]() { *slotF32 = value; - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateState(_state); }); } else if (slotI32 && *slotI32 != (int32)value) { withStateLock([this, slotI32, value]() { *slotI32 = (int32)value; - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateState(_state); }); } @@ -234,7 +234,7 @@ tresult PLUGIN_API SfizzVstController::setState(IBStream* stream) withStateLock([this, &s]() { _uiState = s; - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateUiState(_uiState); }); @@ -246,8 +246,8 @@ tresult PLUGIN_API SfizzVstController::getState(IBStream* stream) tresult result; withStateLock([this, stream, &result]() { - if (_editor) - _uiState = _editor->getCurrentUiState(); + if (IPtr editor = _editor.lock()) + _uiState = editor->getCurrentUiState(); result = _uiState.store(stream); }); @@ -272,7 +272,7 @@ tresult PLUGIN_API SfizzVstController::setComponentState(IBStream* stream) withStateLock([this, &s]() { _state = s; - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateState(_state); }); @@ -300,7 +300,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message) withStateLock([this, data, size]() { _state.sfzFile.assign(static_cast(data), size); - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateState(_state); }); } @@ -314,7 +314,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message) withStateLock([this, data, size]() { _state.scalaFile.assign(static_cast(data), size); - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updateState(_state); }); } @@ -328,7 +328,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message) withStateLock([this, data]() { _playState = *static_cast(data); - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->updatePlayState(_playState); }); } @@ -340,7 +340,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message) if (result != kResultTrue) return result; - if (SfizzVstEditor* editor = _editor) + if (IPtr editor = _editor.lock()) editor->receiveMessage(data, size); } diff --git a/vst/SfizzVstController.h b/vst/SfizzVstController.h index 9e5ba8d4..42094b8f 100644 --- a/vst/SfizzVstController.h +++ b/vst/SfizzVstController.h @@ -9,6 +9,7 @@ #include "public.sdk/source/vst/vsteditcontroller.h" #include "public.sdk/source/vst/vstparameters.h" #include "vstgui/plugin-bindings/vst3editor.h" +#include "WeakPtr.h" #include #include class SfizzVstState; @@ -66,5 +67,5 @@ private: SfizzVstState _state {}; SfizzUiState _uiState {}; // updated on UI open/close/state-request SfizzPlayState _playState {}; - Steinberg::IPtr _editor; + WeakPtr _editor; };