diff --git a/Source/Thunder/PluginServer.cpp b/Source/Thunder/PluginServer.cpp index eea2d7c86..b86da1bbd 100644 --- a/Source/Thunder/PluginServer.cpp +++ b/Source/Thunder/PluginServer.cpp @@ -1203,6 +1203,7 @@ namespace PluginHost { , _service() , _requestClose(false) , _jobs() + , _serviceCleanedUp(false) { TRACE(Activity, (_T("Construct a link with ID: [%d] to [%s]"), Id(), remoteId.QualifiedName().c_str())); @@ -1214,11 +1215,8 @@ namespace PluginHost { TRACE(Activity, (_T("Destruct a link with ID [%d] to [%s]"), Id(), RemoteId().c_str())); // If we are still atatched to a service, detach, we are out of scope... - if (_service.IsValid() == true) { - _service->Unsubscribe(*this); + CleanupService(); - _service.Release(); - } if (_security != nullptr) { _security->Release(); _security = nullptr; diff --git a/Source/Thunder/PluginServer.h b/Source/Thunder/PluginServer.h index 674d2d849..56ec288ee 100644 --- a/Source/Thunder/PluginServer.h +++ b/Source/Thunder/PluginServer.h @@ -25,6 +25,7 @@ #include "IRemoteInstantiation.h" #include "WarningReportingCategories.h" #include "PostMortem.h" +#include #ifndef HOSTING_COMPROCESS #error "Please define the name of the COM process!!!" @@ -5353,11 +5354,8 @@ namespace PluginHost { // If we are closing (or closed) do the clean up if (IsOpen() == false) { - if (_service.IsValid() == true) { - _service->Detach(*this); - _service.Release(); - } + CleanupService(); State(CLOSED, false); @@ -5427,11 +5425,24 @@ namespace PluginHost { SetId(id); } + void CleanupService() + { + bool expected = false; + if (_serviceCleanedUp.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) { + if (_service.IsValid() == true) { + _service->Unsubscribe(*this); + _service->Detach(*this); + _service.Release(); + } + } + } + Server& _parent; PluginHost::ISecurity* _security; Core::ProxyType _service; bool _requestClose; Jobs _jobs; + std::atomic _serviceCleanedUp; // Factories for creating jobs that can be placed on the PluginHost Worker pool. static Core::ProxyPoolType _webJobs;