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);
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();
});
}

View File

@ -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();
});
}

View File

@ -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<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
[this](QBluetoothDeviceDiscoveryAgent::Error newError){
q, [this](QBluetoothDeviceDiscoveryAgent::Error newError){
this->_q_deviceDiscoveryError(newError);
});
}

View File

@ -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<QBluetoothUuid>& uuids) {
q, [this](const QBluetoothAddress &address, const QList<QBluetoothUuid>& 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;

View File

@ -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<int, QProcess::ExitStatus>::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<ServiceMap> 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);
});
}