From 1b8383bee1461fd0f55466e6c5f22ebab736a6e2 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 14 Apr 2018 23:16:55 +0200 Subject: [PATCH] Guarding QTimer::singelShot operations, avoid to access dangling pointers --- src/blackcore/airspacemonitor.cpp | 4 ++++ src/blackcore/webdataservices.cpp | 7 ++++++- src/blackgui/components/configurationwizard.cpp | 7 ++++++- src/blackgui/components/dbownmodelsetcomponent.cpp | 5 +++-- src/blackgui/components/downloaddialog.cpp | 7 ++++++- src/blackgui/components/flightplancomponent.cpp | 4 ++++ src/blackgui/components/installxswiftbuscomponent.cpp | 7 ++++++- src/blackgui/views/viewbase.cpp | 7 ++++++- src/plugins/simulator/emulated/simulatoremulated.cpp | 4 ++++ src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp | 8 +++++++- 10 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/blackcore/airspacemonitor.cpp b/src/blackcore/airspacemonitor.cpp index 75f2d88d8..c83658fee 100644 --- a/src/blackcore/airspacemonitor.cpp +++ b/src/blackcore/airspacemonitor.cpp @@ -119,8 +119,10 @@ namespace BlackCore if (m_network && sApp && !sApp->isShuttingDown()) { // thread safe update of m_network + const QPointer guard(this); QTimer::singleShot(0, m_network, [ = ] { + if (guard.isNull()) { return; } if (m_network) { m_network->addInterimPositionReceiver(callsign); } }); } @@ -459,8 +461,10 @@ namespace BlackCore if (trial < 2 && !complete) { this->addReverseLookupMessage(callsign, "Wait for further data"); + const QPointer guard(this); QTimer::singleShot(1500, this, [ = ]() { + if (guard.isNull()) { return; } this->sendReadyForModelMatching(callsign, trial + 1); // recursive }); return; diff --git a/src/blackcore/webdataservices.cpp b/src/blackcore/webdataservices.cpp index 3704aace9..f0eb02aa9 100644 --- a/src/blackcore/webdataservices.cpp +++ b/src/blackcore/webdataservices.cpp @@ -1179,7 +1179,12 @@ namespace BlackCore // and trigger read if (sApp->isInternetAccessible()) { - QTimer::singleShot(0, m_sharedInfoDataReader, [this]() { m_sharedInfoDataReader->read(); }); + const QPointer guard(this); + QTimer::singleShot(0, m_sharedInfoDataReader, [ = ]() + { + if (guard.isNull()) { return; } + m_sharedInfoDataReader->read(); + }); } else { diff --git a/src/blackgui/components/configurationwizard.cpp b/src/blackgui/components/configurationwizard.cpp index f68002aca..bfb83bd0f 100644 --- a/src/blackgui/components/configurationwizard.cpp +++ b/src/blackgui/components/configurationwizard.cpp @@ -69,7 +69,12 @@ namespace BlackGui bool CConfigurationWizard::event(QEvent *event) { if (event->type() != QEvent::EnterWhatsThisMode) { return QDialog::event(event); } - QTimer::singleShot(0, this, [ = ] { sGui->showHelp(this); }); + const QPointer guard(this); + QTimer::singleShot(0, this, [ = ] + { + if (guard.isNull() || !sGui || sGui->isShuttingDown()) { return; } + sGui->showHelp(this); + }); return true; } diff --git a/src/blackgui/components/dbownmodelsetcomponent.cpp b/src/blackgui/components/dbownmodelsetcomponent.cpp index 0b3923165..a2c33929d 100644 --- a/src/blackgui/components/dbownmodelsetcomponent.cpp +++ b/src/blackgui/components/dbownmodelsetcomponent.cpp @@ -87,9 +87,10 @@ namespace BlackGui { ui->comp_SimulatorSelector->setValue(simulator); ui->le_Simulator->setText(simulator.toQString(true)); - QTimer::singleShot(500, [this]() + const QPointer guard(this); + QTimer::singleShot(500, [ = ]() { - if (!sApp || sApp->isShuttingDown()) { return; } + if (guard.isNull() || !sApp || sApp->isShuttingDown()) { return; } this->updateViewToCurrentModels(); }); } diff --git a/src/blackgui/components/downloaddialog.cpp b/src/blackgui/components/downloaddialog.cpp index dca08e1b5..698240ee9 100644 --- a/src/blackgui/components/downloaddialog.cpp +++ b/src/blackgui/components/downloaddialog.cpp @@ -50,7 +50,12 @@ namespace BlackGui void CDownloadDialog::showAndStartDownloading() { - QTimer::singleShot(0, this, [ = ] { ui->comp_Download->triggerDownloadingOfFiles(2500); }); + const QPointer guard(this); + QTimer::singleShot(0, this, [ = ] + { + if (guard.isNull()) { return; } + ui->comp_Download->triggerDownloadingOfFiles(2500); + }); this->show(); } diff --git a/src/blackgui/components/flightplancomponent.cpp b/src/blackgui/components/flightplancomponent.cpp index 950a12359..3f9092958 100644 --- a/src/blackgui/components/flightplancomponent.cpp +++ b/src/blackgui/components/flightplancomponent.cpp @@ -659,8 +659,10 @@ namespace BlackGui { if (ui->cb_Heavy->isChecked()) { + const QPointer guard(this); QTimer::singleShot(10, this, [ = ] { + if (guard.isNull()) { return; } ui->cb_Tcas->setChecked(false); this->buildPrefixIcaoSuffix(); }); @@ -671,8 +673,10 @@ namespace BlackGui { if (ui->cb_Tcas->isChecked()) { + const QPointer guard(this); QTimer::singleShot(10, this, [ = ] { + if (guard.isNull()) { return; } ui->cb_Heavy->setChecked(false); this->buildPrefixIcaoSuffix(); }); diff --git a/src/blackgui/components/installxswiftbuscomponent.cpp b/src/blackgui/components/installxswiftbuscomponent.cpp index d2e13a940..8c29cc36d 100644 --- a/src/blackgui/components/installxswiftbuscomponent.cpp +++ b/src/blackgui/components/installxswiftbuscomponent.cpp @@ -218,7 +218,12 @@ namespace BlackGui if (reply == QMessageBox::Yes) { const CStatusMessage msg = CStatusMessage(this).info("Using existing file '%1'") << saveAsFile; - QTimer::singleShot(100, this, [ = ] { this->downloadedXSwiftBusFile(msg); }); + const QPointer guard(this); + QTimer::singleShot(100, this, [ = ] + { + if (guard.isNull()) { return; } + this->downloadedXSwiftBusFile(msg); + }); return; } } diff --git a/src/blackgui/views/viewbase.cpp b/src/blackgui/views/viewbase.cpp index c86be21db..33eb8fcd0 100644 --- a/src/blackgui/views/viewbase.cpp +++ b/src/blackgui/views/viewbase.cpp @@ -508,7 +508,12 @@ namespace BlackGui // call this deferred, otherwise the values are overridden with any values // from the UI builder - QTimer::singleShot(500, this, &CViewBaseNonTemplate::settingsChanged); + const QPointer guard(this); + QTimer::singleShot(500, this, [ = ]() + { + if (guard.isNull()) { return; } + CViewBaseNonTemplate::settingsChanged(); + }); } QString CViewBaseNonTemplate::getSettingsFileName(bool load) const diff --git a/src/plugins/simulator/emulated/simulatoremulated.cpp b/src/plugins/simulator/emulated/simulatoremulated.cpp index a59058560..14ebe7035 100644 --- a/src/plugins/simulator/emulated/simulatoremulated.cpp +++ b/src/plugins/simulator/emulated/simulatoremulated.cpp @@ -59,8 +59,10 @@ namespace BlackSimPlugin bool CSimulatorEmulated::connectTo() { + const QPointer guard(this); QTimer::singleShot(1000, this, [ = ] { + if (guard.isNull()) { return; } this->emitSimulatorCombinedStatus(); m_monitorWidget->show(); }); @@ -439,8 +441,10 @@ namespace BlackSimPlugin void CSimulatorEmulatedListener::startImpl() { if (this->isShuttingDown()) { return; } + const QPointer guard(this); QTimer::singleShot(2000, this, [ = ] { + if (guard.isNull()) { return; } Q_ASSERT_X(this->getPluginInfo().isValid(), Q_FUNC_INFO, "Invalid plugin"); emit this->simulatorStarted(this->getPluginInfo()); }); diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index 7be4eae72..0171449d3 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -25,6 +25,7 @@ #include "blackconfig/buildconfig.h" #include +#include #include using namespace BlackConfig; @@ -1004,7 +1005,12 @@ namespace BlackSimPlugin } // cleanup function, actually this should not be needed - QTimer::singleShot(100, this, &CSimulatorFsxCommon::physicallyRemoveAircraftNotInProvider); + const QPointer guard(this); + QTimer::singleShot(100, this, [ = ] + { + if (guard.isNull()) { return; } + CSimulatorFsxCommon::physicallyRemoveAircraftNotInProvider(); + }); // bye return true;