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 819bb06c2c.

This was discovered while investigating QTBUG-71479.

Change-Id: I3a49916ce6d9425c684863bb0b04a10bd3e652b9
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
This commit is contained in:
Alex Blasche 2018-10-30 16:56:38 +01:00 committed by Jani Heikkinen
parent 8d279a05f2
commit c2b510909d
5 changed files with 20 additions and 17 deletions

View File

@ -75,6 +75,7 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(
QDBusConnection::systemBus(), parent); QDBusConnection::systemBus(), parent);
QObject::connect(managerBluez5, QObject::connect(managerBluez5,
&OrgFreedesktopDBusObjectManagerInterface::InterfacesAdded, &OrgFreedesktopDBusObjectManagerInterface::InterfacesAdded,
q_ptr,
[this](const QDBusObjectPath &objectPath, InterfaceList interfacesAndProperties) { [this](const QDBusObjectPath &objectPath, InterfaceList interfacesAndProperties) {
this->_q_InterfacesAdded(objectPath, interfacesAndProperties); this->_q_InterfacesAdded(objectPath, interfacesAndProperties);
}); });
@ -85,7 +86,7 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(
manager = new OrgBluezManagerInterface(QStringLiteral("org.bluez"), QStringLiteral("/"), manager = new OrgBluezManagerInterface(QStringLiteral("org.bluez"), QStringLiteral("/"),
QDBusConnection::systemBus(), parent); QDBusConnection::systemBus(), parent);
QObject::connect(&extendedDiscoveryTimer, QObject::connect(&extendedDiscoveryTimer,
&QTimer::timeout, [this]() { &QTimer::timeout, q_ptr, [this]() {
this->_q_extendedDeviceDiscoveryTimeout(); this->_q_extendedDeviceDiscoveryTimeout();
}); });
extendedDiscoveryTimer.setInterval(10000); extendedDiscoveryTimer.setInterval(10000);
@ -162,11 +163,11 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent
Q_Q(QBluetoothDeviceDiscoveryAgent); Q_Q(QBluetoothDeviceDiscoveryAgent);
QObject::connect(adapter, &OrgBluezAdapterInterface::DeviceFound, 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); this->_q_deviceFound(address, dict);
}); });
QObject::connect(adapter, &OrgBluezAdapterInterface::PropertyChanged, 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); this->_q_propertyChanged(name, value);
}); });
@ -287,7 +288,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5(QBluetoothDeviceDiscover
QtBluezDiscoveryManager::instance()->registerDiscoveryInterest(adapterBluez5->path()); QtBluezDiscoveryManager::instance()->registerDiscoveryInterest(adapterBluez5->path());
QObject::connect(QtBluezDiscoveryManager::instance(), &QtBluezDiscoveryManager::discoveryInterrupted, QObject::connect(QtBluezDiscoveryManager::instance(), &QtBluezDiscoveryManager::discoveryInterrupted,
[this](const QString &path){ q, [this](const QString &path){
this->_q_discoveryInterrupted(path); this->_q_discoveryInterrupted(path);
}); });
@ -321,7 +322,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5(QBluetoothDeviceDiscover
discoveryTimer = new QTimer(q); discoveryTimer = new QTimer(q);
discoveryTimer->setSingleShot(true); discoveryTimer->setSingleShot(true);
QObject::connect(discoveryTimer, &QTimer::timeout, QObject::connect(discoveryTimer, &QTimer::timeout,
[this]() { q, [this]() {
this->_q_discoveryFinished(); this->_q_discoveryFinished();
}); });
} }

View File

@ -253,7 +253,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port)
d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(), d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(),
QSocketNotifier::Read); QSocketNotifier::Read);
connect(d->socketNotifier, &QSocketNotifier::activated, connect(d->socketNotifier, &QSocketNotifier::activated,
[d](){ this, [d](){
d->_q_newConnection(); d->_q_newConnection();
}); });
} }

View File

@ -435,16 +435,16 @@ void QBluetoothServiceDiscoveryAgentPrivate::startDeviceDiscovery()
deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(q); deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(q);
#endif #endif
QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished,
[this](){ q, [this](){
this->_q_deviceDiscoveryFinished(); this->_q_deviceDiscoveryFinished();
}); });
QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
[this](const QBluetoothDeviceInfo &info){ q, [this](const QBluetoothDeviceInfo &info){
this->_q_deviceDiscovered(info); this->_q_deviceDiscovered(info);
}); });
QObject::connect(deviceDiscoveryAgent, QObject::connect(deviceDiscoveryAgent,
QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
[this](QBluetoothDeviceDiscoveryAgent::Error newError){ q, [this](QBluetoothDeviceDiscoveryAgent::Error newError){
this->_q_deviceDiscoveryError(newError); this->_q_deviceDiscoveryError(newError);
}); });
} }

View File

@ -200,7 +200,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr
if (!receiver) { if (!receiver) {
receiver = new ServiceDiscoveryBroadcastReceiver(); receiver = new ServiceDiscoveryBroadcastReceiver();
QObject::connect(receiver, &ServiceDiscoveryBroadcastReceiver::uuidFetchFinished, QObject::connect(receiver, &ServiceDiscoveryBroadcastReceiver::uuidFetchFinished,
[this](const QBluetoothAddress &address, const QList<QBluetoothUuid>& uuids) { q, [this](const QBluetoothAddress &address, const QList<QBluetoothUuid>& uuids) {
this->_q_processFetchedUuids(address, uuids); this->_q_processFetchedUuids(address, uuids);
}); });
} }
@ -208,7 +208,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr
if (!localDeviceReceiver) { if (!localDeviceReceiver) {
localDeviceReceiver = new LocalDeviceBroadcastReceiver(); localDeviceReceiver = new LocalDeviceBroadcastReceiver();
QObject::connect(localDeviceReceiver, &LocalDeviceBroadcastReceiver::hostModeStateChanged, QObject::connect(localDeviceReceiver, &LocalDeviceBroadcastReceiver::hostModeStateChanged,
[this](QBluetoothLocalDevice::HostMode state){ q, [this](QBluetoothLocalDevice::HostMode state){
this->_q_hostModeStateChanged(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 //could not find any service for the current address/device -> go to next one
if (address.isNull() || uuids.isEmpty()) { if (address.isNull() || uuids.isEmpty()) {
if (discoveredDevices.count() == 1) { if (discoveredDevices.count() == 1) {
QTimer::singleShot(4000, qApp, [this]() { Q_Q(QBluetoothServiceDiscoveryAgent);
QTimer::singleShot(4000, q, [this]() {
this->_q_fetchUuidsTimeout(); this->_q_fetchUuidsTimeout();
}); });
} }
@ -301,7 +302,8 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_processFetchedUuids(
//the discovery on the last device cannot immediately finish //the discovery on the last device cannot immediately finish
//we have to grant the 2 seconds timeout delay //we have to grant the 2 seconds timeout delay
if (discoveredDevices.count() == 1) { if (discoveredDevices.count() == 1) {
QTimer::singleShot(4000, qApp, [this]() { Q_Q(QBluetoothServiceDiscoveryAgent);
QTimer::singleShot(4000, q, [this]() {
this->_q_fetchUuidsTimeout(); this->_q_fetchUuidsTimeout();
}); });
return; return;

View File

@ -129,7 +129,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(deviceObjectPath, q); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(deviceObjectPath, q);
watcher->setProperty("_q_BTaddress", QVariant::fromValue(address)); watcher->setProperty("_q_BTaddress", QVariant::fromValue(address));
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
[this](QDBusPendingCallWatcher *watcher){ q, [this](QDBusPendingCallWatcher *watcher){
this->_q_foundDevice(watcher); this->_q_foundDevice(watcher);
}); });
} }
@ -216,7 +216,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::runExternalSdpScan(
sdpScannerProcess->setProgram(fileInfo.canonicalFilePath()); sdpScannerProcess->setProgram(fileInfo.canonicalFilePath());
q->connect(sdpScannerProcess, q->connect(sdpScannerProcess,
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this](int exitCode, QProcess::ExitStatus status){ q, [this](int exitCode, QProcess::ExitStatus status){
this->_q_sdpScannerDone(exitCode, status); this->_q_sdpScannerDone(exitCode, status);
}); });
} }
@ -396,7 +396,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_foundDevice(QDBusPendingCallWatc
watcher = new QDBusPendingCallWatcher(deviceObjectPath, q); watcher = new QDBusPendingCallWatcher(deviceObjectPath, q);
watcher->setProperty("_q_BTaddress", QVariant::fromValue(address)); watcher->setProperty("_q_BTaddress", QVariant::fromValue(address));
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
[this](QDBusPendingCallWatcher *watcher){ q, [this](QDBusPendingCallWatcher *watcher){
this->_q_createdDevice(watcher); this->_q_createdDevice(watcher);
}); });
@ -537,7 +537,7 @@ void QBluetoothServiceDiscoveryAgentPrivate::discoverServices(const QString &dev
QDBusPendingReply<ServiceMap> discoverReply = device->DiscoverServices(pattern); QDBusPendingReply<ServiceMap> discoverReply = device->DiscoverServices(pattern);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(discoverReply, q); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(discoverReply, q);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
[this](QDBusPendingCallWatcher *watcher){ q, [this](QDBusPendingCallWatcher *watcher){
this->_q_discoveredServices(watcher); this->_q_discoveredServices(watcher);
}); });
} }