From c2b510909dc8839ff6578086051a22e8b21462e3 Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Tue, 30 Oct 2018 16:56:38 +0100 Subject: Ensure QObject context is retained to avoid crashes If the public class is deleted pending lambda invocation can cause crashes unless QObject context is provided. This fixes a regression introduced by 819bb06c2cb3372cb1bb9ddd7f3a504f78d3452d. This was discovered while investigating QTBUG-71479. Change-Id: I3a49916ce6d9425c684863bb0b04a10bd3e652b9 Reviewed-by: Oliver Wolff --- src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp | 11 ++++++----- src/bluetooth/qbluetoothserver_bluez.cpp | 2 +- src/bluetooth/qbluetoothservicediscoveryagent.cpp | 6 +++--- src/bluetooth/qbluetoothservicediscoveryagent_android.cpp | 10 ++++++---- src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp | 8 ++++---- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp index 51546df2..bbb4ac3e 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp @@ -75,6 +75,7 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( QDBusConnection::systemBus(), parent); QObject::connect(managerBluez5, &OrgFreedesktopDBusObjectManagerInterface::InterfacesAdded, + q_ptr, [this](const QDBusObjectPath &objectPath, InterfaceList interfacesAndProperties) { this->_q_InterfacesAdded(objectPath, interfacesAndProperties); }); @@ -85,7 +86,7 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( manager = new OrgBluezManagerInterface(QStringLiteral("org.bluez"), QStringLiteral("/"), QDBusConnection::systemBus(), parent); QObject::connect(&extendedDiscoveryTimer, - &QTimer::timeout, [this]() { + &QTimer::timeout, q_ptr, [this]() { this->_q_extendedDeviceDiscoveryTimeout(); }); extendedDiscoveryTimer.setInterval(10000); @@ -162,11 +163,11 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent Q_Q(QBluetoothDeviceDiscoveryAgent); QObject::connect(adapter, &OrgBluezAdapterInterface::DeviceFound, - [this](const QString &address, const QVariantMap &dict) { + q, [this](const QString &address, const QVariantMap &dict) { this->_q_deviceFound(address, dict); }); QObject::connect(adapter, &OrgBluezAdapterInterface::PropertyChanged, - [this](const QString &name, const QDBusVariant &value) { + q, [this](const QString &name, const QDBusVariant &value) { this->_q_propertyChanged(name, value); }); @@ -287,7 +288,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5(QBluetoothDeviceDiscover QtBluezDiscoveryManager::instance()->registerDiscoveryInterest(adapterBluez5->path()); QObject::connect(QtBluezDiscoveryManager::instance(), &QtBluezDiscoveryManager::discoveryInterrupted, - [this](const QString &path){ + q, [this](const QString &path){ this->_q_discoveryInterrupted(path); }); @@ -321,7 +322,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5(QBluetoothDeviceDiscover discoveryTimer = new QTimer(q); discoveryTimer->setSingleShot(true); QObject::connect(discoveryTimer, &QTimer::timeout, - [this]() { + q, [this]() { this->_q_discoveryFinished(); }); } diff --git a/src/bluetooth/qbluetoothserver_bluez.cpp b/src/bluetooth/qbluetoothserver_bluez.cpp index f45aee28..54bc85a0 100644 --- a/src/bluetooth/qbluetoothserver_bluez.cpp +++ b/src/bluetooth/qbluetoothserver_bluez.cpp @@ -253,7 +253,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(), QSocketNotifier::Read); connect(d->socketNotifier, &QSocketNotifier::activated, - [d](){ + this, [d](){ d->_q_newConnection(); }); } diff --git a/src/bluetooth/qbluetoothservicediscoveryagent.cpp b/src/bluetooth/qbluetoothservicediscoveryagent.cpp index 3260d7db..390b9f74 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent.cpp @@ -435,16 +435,16 @@ void QBluetoothServiceDiscoveryAgentPrivate::startDeviceDiscovery() deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(q); #endif QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, - [this](){ + q, [this](){ this->_q_deviceDiscoveryFinished(); }); QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, - [this](const QBluetoothDeviceInfo &info){ + q, [this](const QBluetoothDeviceInfo &info){ this->_q_deviceDiscovered(info); }); QObject::connect(deviceDiscoveryAgent, QOverload::of(&QBluetoothDeviceDiscoveryAgent::error), - [this](QBluetoothDeviceDiscoveryAgent::Error newError){ + q, [this](QBluetoothDeviceDiscoveryAgent::Error newError){ this->_q_deviceDiscoveryError(newError); }); } diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp index 7ab1f50c..ce2911d3 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp @@ -200,7 +200,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr if (!receiver) { receiver = new ServiceDiscoveryBroadcastReceiver(); QObject::connect(receiver, &ServiceDiscoveryBroadcastReceiver::uuidFetchFinished, - [this](const QBluetoothAddress &address, const QList& uuids) { + q, [this](const QBluetoothAddress &address, const QList& uuids) { this->_q_processFetchedUuids(address, uuids); }); } @@ -208,7 +208,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr if (!localDeviceReceiver) { localDeviceReceiver = new LocalDeviceBroadcastReceiver(); QObject::connect(localDeviceReceiver, &LocalDeviceBroadcastReceiver::hostModeStateChanged, - [this](QBluetoothLocalDevice::HostMode state){ + q, [this](QBluetoothLocalDevice::HostMode state){ this->_q_hostModeStateChanged(state); }); } @@ -250,7 +250,8 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_processFetchedUuids( //could not find any service for the current address/device -> go to next one if (address.isNull() || uuids.isEmpty()) { if (discoveredDevices.count() == 1) { - QTimer::singleShot(4000, qApp, [this]() { + Q_Q(QBluetoothServiceDiscoveryAgent); + QTimer::singleShot(4000, q, [this]() { this->_q_fetchUuidsTimeout(); }); } @@ -301,7 +302,8 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_processFetchedUuids( //the discovery on the last device cannot immediately finish //we have to grant the 2 seconds timeout delay if (discoveredDevices.count() == 1) { - QTimer::singleShot(4000, qApp, [this]() { + Q_Q(QBluetoothServiceDiscoveryAgent); + QTimer::singleShot(4000, q, [this]() { this->_q_fetchUuidsTimeout(); }); return; diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp index dbd084ed..e16caff6 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp @@ -129,7 +129,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(deviceObjectPath, q); watcher->setProperty("_q_BTaddress", QVariant::fromValue(address)); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, - [this](QDBusPendingCallWatcher *watcher){ + q, [this](QDBusPendingCallWatcher *watcher){ this->_q_foundDevice(watcher); }); } @@ -216,7 +216,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::runExternalSdpScan( sdpScannerProcess->setProgram(fileInfo.canonicalFilePath()); q->connect(sdpScannerProcess, QOverload::of(&QProcess::finished), - [this](int exitCode, QProcess::ExitStatus status){ + q, [this](int exitCode, QProcess::ExitStatus status){ this->_q_sdpScannerDone(exitCode, status); }); } @@ -396,7 +396,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_foundDevice(QDBusPendingCallWatc watcher = new QDBusPendingCallWatcher(deviceObjectPath, q); watcher->setProperty("_q_BTaddress", QVariant::fromValue(address)); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, - [this](QDBusPendingCallWatcher *watcher){ + q, [this](QDBusPendingCallWatcher *watcher){ this->_q_createdDevice(watcher); }); @@ -537,7 +537,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::discoverServices(const QString &dev QDBusPendingReply discoverReply = device->DiscoverServices(pattern); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(discoverReply, q); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, - [this](QDBusPendingCallWatcher *watcher){ + q, [this](QDBusPendingCallWatcher *watcher){ this->_q_discoveredServices(watcher); }); } -- cgit v1.2.3