From fd9662b8034190ac4c3e55bbad6aad963d83e492 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 25 Sep 2017 11:55:18 +0200 Subject: [PATCH] bandwidthmanager: Fix unregistering devices on delete 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 | 24 +++++------------------- src/libsync/bandwidthmanager.h | 2 -- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/libsync/bandwidthmanager.cpp b/src/libsync/bandwidthmanager.cpp index 0393b353f..c1ed6adcd 100644 --- a/src/libsync/bandwidthmanager.cpp +++ b/src/libsync/bandwidthmanager.cpp @@ -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(o); - if (p) { - unregisterUploadDevice(p); - } -} - -void BandwidthManager::unregisterUploadDevice(UploadDevice *p) -{ + auto p = reinterpret_cast(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(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(o); - if (p) { - unregisterDownloadJob(p); - } -} - void BandwidthManager::relativeUploadMeasuringTimerExpired() { if (!usingRelativeUploadLimit() || _relativeUploadDeviceList.count() == 0) { diff --git a/src/libsync/bandwidthmanager.h b/src/libsync/bandwidthmanager.h index 77a327287..691e11162 100644 --- a/src/libsync/bandwidthmanager.h +++ b/src/libsync/bandwidthmanager.h @@ -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();