bandwidthmanager: Fix unregistering devices on delete
authorOlivier Goffart <ogoffart@woboq.com>
Mon, 25 Sep 2017 09:55:18 +0000 (11:55 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 5 Oct 2017 20:01:36 +0000 (22:01 +0200)
from the destroyed signal, qobject_cast won't work because the object
is already destroyed. One must use reinterpret_cast then

src/libsync/bandwidthmanager.cpp
src/libsync/bandwidthmanager.h

index 0393b353f780fa3d5cec48a3b3b4080c6608449e..c1ed6adcd175c3c681e6c87d4f4f2a308888a431 100644 (file)
@@ -95,7 +95,7 @@ void BandwidthManager::registerUploadDevice(UploadDevice *p)
 {
     _absoluteUploadDeviceList.append(p);
     _relativeUploadDeviceList.append(p);
-    QObject::connect(p, SIGNAL(destroyed(QObject *)), this, SLOT(unregisterUploadDevice(QObject *)));
+    QObject::connect(p, &QObject::destroyed, this, &BandwidthManager::unregisterUploadDevice);
 
     if (usingAbsoluteUploadLimit()) {
         p->setBandwidthLimited(true);
@@ -111,14 +111,7 @@ void BandwidthManager::registerUploadDevice(UploadDevice *p)
 
 void BandwidthManager::unregisterUploadDevice(QObject *o)
 {
-    UploadDevice *p = qobject_cast<UploadDevice *>(o);
-    if (p) {
-        unregisterUploadDevice(p);
-    }
-}
-
-void BandwidthManager::unregisterUploadDevice(UploadDevice *p)
-{
+    auto p = reinterpret_cast<UploadDevice *>(o); // note, we might already be in the ~QObject
     _absoluteUploadDeviceList.removeAll(p);
     _relativeUploadDeviceList.removeAll(p);
     if (p == _relativeLimitCurrentMeasuredDevice) {
@@ -130,7 +123,7 @@ void BandwidthManager::unregisterUploadDevice(UploadDevice *p)
 void BandwidthManager::registerDownloadJob(GETFileJob *j)
 {
     _downloadJobList.append(j);
-    QObject::connect(j, SIGNAL(destroyed(QObject *)), this, SLOT(unregisterDownloadJob(QObject *)));
+    QObject::connect(j, &QObject::destroyed, this, &BandwidthManager::unregisterDownloadJob);
 
     if (usingAbsoluteDownloadLimit()) {
         j->setBandwidthLimited(true);
@@ -144,8 +137,9 @@ void BandwidthManager::registerDownloadJob(GETFileJob *j)
     }
 }
 
-void BandwidthManager::unregisterDownloadJob(GETFileJob *j)
+void BandwidthManager::unregisterDownloadJob(QObject *o)
 {
+    GETFileJob *j = reinterpret_cast<GETFileJob *>(o); // note, we might already be in the ~QObject
     _downloadJobList.removeAll(j);
     if (_relativeLimitCurrentMeasuredJob == j) {
         _relativeLimitCurrentMeasuredJob = 0;
@@ -153,14 +147,6 @@ void BandwidthManager::unregisterDownloadJob(GETFileJob *j)
     }
 }
 
-void BandwidthManager::unregisterDownloadJob(QObject *o)
-{
-    GETFileJob *p = qobject_cast<GETFileJob *>(o);
-    if (p) {
-        unregisterDownloadJob(p);
-    }
-}
-
 void BandwidthManager::relativeUploadMeasuringTimerExpired()
 {
     if (!usingRelativeUploadLimit() || _relativeUploadDeviceList.count() == 0) {
index 77a32728790b8e3d107212da3dcfda1d472c30fa..691e11162d0879adf71e6b109b6c6cadf069de2a 100644 (file)
@@ -45,11 +45,9 @@ public:
 
 public slots:
     void registerUploadDevice(UploadDevice *);
-    void unregisterUploadDevice(UploadDevice *);
     void unregisterUploadDevice(QObject *);
 
     void registerDownloadJob(GETFileJob *);
-    void unregisterDownloadJob(GETFileJob *);
     void unregisterDownloadJob(QObject *);
 
     void absoluteLimitTimerExpired();