diff --git a/src/devices/devicelister.h b/src/devices/devicelister.h index a821d10ab..a4e0ab6cc 100644 --- a/src/devices/devicelister.h +++ b/src/devices/devicelister.h @@ -57,6 +57,7 @@ public: public slots: virtual void UpdateDeviceFreeSpace(const QString& id) = 0; + virtual void ShutDown() {} signals: void DeviceAdded(const QString& id); diff --git a/src/devices/devicemanager.cpp b/src/devices/devicemanager.cpp index df822d38e..8cda65c85 100644 --- a/src/devices/devicemanager.cpp +++ b/src/devices/devicemanager.cpp @@ -201,7 +201,13 @@ DeviceManager::DeviceManager(BackgroundThread* database, } DeviceManager::~DeviceManager() { - qDeleteAll(listers_); + foreach (DeviceLister* lister, listers_) { + // Let the lister close itself down + metaObject()->invokeMethod(lister, "ShutDown", Qt::BlockingQueuedConnection); + + delete lister; + } + backend_->deleteLater(); } diff --git a/src/devices/wmdmlister.cpp b/src/devices/wmdmlister.cpp index 9492f662b..f41c62d77 100644 --- a/src/devices/wmdmlister.cpp +++ b/src/devices/wmdmlister.cpp @@ -18,8 +18,6 @@ #include #include -#include -#include #include #include @@ -54,15 +52,15 @@ void WmdmLister::Init() { return; } - SacHandle sac = CSecureChannelClient_New(); + sac_ = CSecureChannelClient_New(); if (CSecureChannelClient_SetCertificate( - sac, SAC_CERT_V1, abCert, sizeof(abCert), abPVK, sizeof(abPVK))) { + sac_, SAC_CERT_V1, abCert, sizeof(abCert), abPVK, sizeof(abPVK))) { qWarning() << "Error setting SAC certificate"; return; } - CSecureChannelClient_SetInterface(sac, auth); - if (CSecureChannelClient_Authenticate(sac, SAC_PROTOCOL_V1)) { + CSecureChannelClient_SetInterface(sac_, auth); + if (CSecureChannelClient_Authenticate(sac_, SAC_PROTOCOL_V1)) { qWarning() << "Error authenticating with SAC"; return; } @@ -120,6 +118,27 @@ void WmdmLister::Init() { } } +void WmdmLister::ShutDown() { + // Unregister for notifications + IConnectionPointContainer* cp_container; + device_manager_->QueryInterface(IID_IConnectionPointContainer, (void**)&cp_container); + + IConnectionPoint* cp; + cp_container->FindConnectionPoint(IID_IWMDMNotification, &cp); + + cp->Release(); + cp_container->Release(); + + // Release the device manager + device_manager_->Release(); + + // SAC + CSecureChannelClient_Free(sac_); + + // Uninitialise COM + CoUninitialize(); +} + template qint64 GetSpaceValue(F f) { DWORD low, high; diff --git a/src/devices/wmdmlister.h b/src/devices/wmdmlister.h index a1145b8d4..8f8416843 100644 --- a/src/devices/wmdmlister.h +++ b/src/devices/wmdmlister.h @@ -28,6 +28,7 @@ struct IWMDeviceManager; #include #include +#include #undef LoadIcon @@ -60,6 +61,7 @@ public: public slots: virtual void UpdateDeviceFreeSpace(const QString& id); + virtual void ShutDown(); private: struct DeviceInfo { @@ -89,6 +91,7 @@ private: private: IWMDeviceManager* device_manager_; + SacHandle sac_; DWORD notification_cookie_; QMutex mutex_;