diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 0ae3ae972..4c09748a2 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -648,9 +648,13 @@ namespace BlackCore multiPart->moveToThread(m_accessManager->thread()); } - return httpRequestImpl(request, logId, callback, NoRedirects, [ this, multiPart ](QNetworkAccessManager & qam, const QNetworkRequest & request) + QPointer myself(this); + return httpRequestImpl(request, logId, callback, NoRedirects, [ = ](QNetworkAccessManager & qam, const QNetworkRequest & request) { - QNetworkReply *nr = qam.post(request, multiPart); + QNetworkReply *nr = nullptr; + if (!myself) { return nr; } + if (!multiPart) { return nr; } + nr = qam.post(request, multiPart); multiPart->setParent(nr); return nr; }); diff --git a/src/blackcore/context/contextapplication.cpp b/src/blackcore/context/contextapplication.cpp index 5316fe256..05771b494 100644 --- a/src/blackcore/context/contextapplication.cpp +++ b/src/blackcore/context/contextapplication.cpp @@ -51,29 +51,37 @@ namespace BlackCore CContext(mode, runtime) { if (mode == CCoreFacadeConfig::NotUsed) { return; } - connect(CLogHandler::instance(), &CLogHandler::localMessageLogged, this, [this](const CStatusMessage & message) + QPointer myself(this); + + connect(CLogHandler::instance(), &CLogHandler::localMessageLogged, this, [ = ](const CStatusMessage & message) { + if (!myself) { return; } this->logMessage(message, {}); }); - connect(CLogHandler::instance(), &CLogHandler::subscriptionAdded, this, [this](const CLogPattern & pattern) + connect(CLogHandler::instance(), &CLogHandler::subscriptionAdded, this, [ = ](const CLogPattern & pattern) { + if (!myself) { return; } this->addLogSubscription({}, pattern); }); - connect(CLogHandler::instance(), &CLogHandler::subscriptionRemoved, this, [this](const CLogPattern & pattern) + connect(CLogHandler::instance(), &CLogHandler::subscriptionRemoved, this, [ = ](const CLogPattern & pattern) { + if (!myself) { return; } this->removeLogSubscription({}, pattern); }); - connect(this, &IContextApplication::logSubscriptionAdded, this, [this](const CIdentifier & subscriber, const CLogPattern & pattern) + connect(this, &IContextApplication::logSubscriptionAdded, this, [ = ](const CIdentifier & subscriber, const CLogPattern & pattern) { - this->m_logSubscriptions[subscriber].push_back(pattern); + if (!myself) { return; } + m_logSubscriptions[subscriber].push_back(pattern); }); - connect(this, &IContextApplication::logSubscriptionRemoved, this, [this](const CIdentifier & subscriber, const CLogPattern & pattern) + connect(this, &IContextApplication::logSubscriptionRemoved, this, [ = ](const CIdentifier & subscriber, const CLogPattern & pattern) { - this->m_logSubscriptions[subscriber].removeAll(pattern); + if (!myself) { return; } + m_logSubscriptions[subscriber].removeAll(pattern); }); - connect(CSettingsCache::instance(), &CSettingsCache::valuesChangedByLocal, [this](const CValueCachePacket & settings) + connect(CSettingsCache::instance(), &CSettingsCache::valuesChangedByLocal, [ = ](const CValueCachePacket & settings) { + if (!myself) { return; } this->changeSettings(settings, {}); }); @@ -83,14 +91,15 @@ namespace BlackCore CSettingsCache::instance()->changeValuesFromRemote(settings, origin); }); - bool s = connect(CInputManager::instance(), &CInputManager::hotkeyActionRegistered, [this](const QStringList & actions) + bool s = connect(CInputManager::instance(), &CInputManager::hotkeyActionRegistered, [ = ](const QStringList & actions) { + if (!myself) { return; } this->registerHotkeyActions(actions, {}); }); Q_ASSERT_X(s, Q_FUNC_INFO, "Connect hotkey action failed"); Q_UNUSED(s); - s = connect(this, &IContextApplication::hotkeyActionsRegistered, [this](const QStringList & actions, const CIdentifier & origin) + s = connect(this, &IContextApplication::hotkeyActionsRegistered, [ = ](const QStringList & actions, const CIdentifier & origin) { if (origin.hasApplicationProcessId()) { return; } CInputManager::instance()->registerRemoteActions(actions); @@ -98,15 +107,17 @@ namespace BlackCore Q_ASSERT_X(s, Q_FUNC_INFO, "Connect hotkey actions failed"); Q_UNUSED(s); - s = connect(CInputManager::instance(), &CInputManager::remoteActionFromLocal, [this](const QString & action, bool argument) + s = connect(CInputManager::instance(), &CInputManager::remoteActionFromLocal, [ = ](const QString & action, bool argument) { + if (!myself) { return; } this->callHotkeyAction(action, argument, {}); }); Q_ASSERT_X(s, Q_FUNC_INFO, "Connect remote action failed"); Q_UNUSED(s); - s = connect(this, &IContextApplication::remoteHotkeyAction, [this](const QString & action, bool argument, const CIdentifier & origin) + s = connect(this, &IContextApplication::remoteHotkeyAction, [ = ](const QString & action, bool argument, const CIdentifier & origin) { + if (!myself) { return; } if (origin.isFromLocalMachine()) { return; } CInputManager::instance()->callFunctionsBy(action, argument); CLogMessage(this, CLogCategory::contextSlot()).debug() << "Calling function" << action << "from origin" << origin.getMachineName(); diff --git a/src/blackgui/guiapplication.cpp b/src/blackgui/guiapplication.cpp index 320abdb72..cbe2bf5ca 100644 --- a/src/blackgui/guiapplication.cpp +++ b/src/blackgui/guiapplication.cpp @@ -445,7 +445,8 @@ namespace BlackGui QMenu *sm = menu.addMenu(CIcons::appSettings16(), "Settings"); sm->setIcon(CIcons::appSettings16()); QAction *a = sm->addAction(CIcons::disk16(), "Settings directory"); - bool c = connect(a, &QAction::triggered, this, [a, this]() + QPointer myself(this); + bool c = connect(a, &QAction::triggered, this, [ = ]() { const QString path(QDir::toNativeSeparators(CSettingsCache::persistentStore())); if (QDir(path).exists()) @@ -456,25 +457,27 @@ namespace BlackGui Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = sm->addAction("Reset settings"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ] { + if (myself.isNull()) { return; } CSettingsCache::instance()->clearAllValues(); - this->displayTextInConsole("Cleared all settings!"); + myself->displayTextInConsole("Cleared all settings!"); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = sm->addAction("List settings files"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (myself.isNull()) { return; } const QStringList files(CSettingsCache::instance()->enumerateStore()); - this->displayTextInConsole(files.join("\n")); + myself->displayTextInConsole(files.join("\n")); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); sm = menu.addMenu("Cache"); sm->setIcon(CIcons::appSettings16()); a = sm->addAction(CIcons::disk16(), "Cache directory"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ]() { const QString path(QDir::toNativeSeparators(CDataCache::persistentStore())); if (QDir(path).exists()) @@ -485,23 +488,25 @@ namespace BlackGui Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = sm->addAction("Reset cache"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (myself.isNull()) { return; } const QStringList files = CApplication::clearCaches(); this->displayTextInConsole("Cleared caches! " + QString::number(files.size()) + " files"); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = sm->addAction("List cache files"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (myself.isNull()) { return; } const QStringList files(CDataCache::instance()->enumerateStore()); - this->displayTextInConsole(files.join("\n")); + myself->displayTextInConsole(files.join("\n")); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = menu.addAction(CIcons::disk16(), "Log directory"); - c = connect(a, &QAction::triggered, this, [this]() + c = connect(a, &QAction::triggered, this, [ = ]() { const QString path(QDir::toNativeSeparators(CDirectoryUtils::logDirectory())); if (QDir(path).exists()) @@ -519,18 +524,21 @@ namespace BlackGui void CGuiApplication::addMenuForStyleSheets(QMenu &menu) { + QPointer myself(this); QMenu *sm = menu.addMenu("Style sheet"); QAction *aReload = sm->addAction(CIcons::refresh16(), "Reload"); - bool c = connect(aReload, &QAction::triggered, this, [aReload, this]() + bool c = connect(aReload, &QAction::triggered, this, [ = ]() { - this->reloadStyleSheets(); + if (myself.isNull()) { return; } + myself->reloadStyleSheets(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); QAction *aOpen = sm->addAction(CIcons::text16(), "Open qss file"); - c = connect(aOpen, &QAction::triggered, this, [aOpen, this]() + c = connect(aOpen, &QAction::triggered, this, [ = ]() { - this->openStandardWidgetStyleSheet(); + if (myself.isNull()) { return; } + myself->openStandardWidgetStyleSheet(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); Q_UNUSED(c); @@ -553,11 +561,13 @@ namespace BlackGui menu.addSeparator(); a = menu.addAction("E&xit"); a->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q)); - c = connect(a, &QAction::triggered, this, [a, this]() + QPointer myself(this); + c = connect(a, &QAction::triggered, this, [ = ]() { // a close event might already trigger a shutdown - this->mainApplicationWidget()->close(); - this->gracefulShutdown(); + if (!myself) { return; } + myself->mainApplicationWidget()->close(); + myself->gracefulShutdown(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); Q_UNUSED(c); @@ -565,38 +575,43 @@ namespace BlackGui void CGuiApplication::addMenuInternals(QMenu &menu) { + QPointer myself(this); QMenu *sm = menu.addMenu("Templates"); QAction *a = sm->addAction("JSON bootstrap"); - bool c = connect(a, &QAction::triggered, this, [a, this]() + bool c = connect(a, &QAction::triggered, this, [ = ]() { + if (!myself) { return; } const CGlobalSetup s = this->getGlobalSetup(); this->displayTextInConsole(s.toJsonString()); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = sm->addAction("JSON update info (for info only)"); - c = connect(a, &QAction::triggered, this, [a, this]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (!myself) { return; } const CUpdateInfo info = this->getUpdateInfo(); - this->displayTextInConsole(info.toJsonString()); + myself->displayTextInConsole(info.toJsonString()); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); if (this->hasWebDataServices()) { a = menu.addAction("Services log.(console)"); - c = connect(a, &QAction::triggered, this, [a, this]() + c = connect(a, &QAction::triggered, this, [ = ]() { - this->displayTextInConsole(this->getWebDataServices()->getReadersLog()); + if (!myself) { return; } + myself->displayTextInConsole(this->getWebDataServices()->getReadersLog()); CLogMessage(this).info("Displayed services log."); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); } a = menu.addAction("Metadata (slow)"); - c = connect(a, &QAction::triggered, this, [a, this]() + c = connect(a, &QAction::triggered, this, [ = ]() { - this->displayTextInConsole(getAllUserMetatypesTypes()); + if (!myself) { return; } + myself->displayTextInConsole(getAllUserMetatypesTypes()); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); Q_UNUSED(c); @@ -604,44 +619,44 @@ namespace BlackGui void CGuiApplication::addMenuWindow(QMenu &menu) { - QWidget *w = mainApplicationWidget(); + QPointer w = mainApplicationWidget(); if (!w) { return; } const QSize iconSize = CIcons::empty16().size(); QPixmap icon = w->style()->standardIcon(QStyle::SP_TitleBarMaxButton).pixmap(iconSize); QAction *a = menu.addAction(icon.scaled(iconSize), "Fullscreen"); - bool c = connect(a, &QAction::triggered, this, [a, w]() + bool c = connect(a, &QAction::triggered, this, [ = ]() { + if (!w) { return; } w->showFullScreen(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); icon = w->style()->standardIcon(QStyle::SP_TitleBarMinButton).pixmap(iconSize); a = menu.addAction(icon.scaled(iconSize), "Minimize"); - c = connect(a, &QAction::triggered, this, [a, w]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (!w) { return; } w->showMinimized(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); icon = w->style()->standardIcon(QStyle::SP_TitleBarNormalButton).pixmap(iconSize); a = menu.addAction(icon.scaled(iconSize), "Normal"); - c = connect(a, &QAction::triggered, this, [a, w]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (!w) { return; } w->showNormal(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = menu.addAction("Toggle stay on top"); - c = connect(a, &QAction::triggered, this, [a, w]() + c = connect(a, &QAction::triggered, this, [ = ]() { - if (CGuiUtility::toggleStayOnTop(w)) - { - CLogMessage(w).info("Window on top"); - } - else - { - CLogMessage(w).info("Window not always on top"); - } + if (!w) { return; } + const bool onTop = CGuiUtility::toggleStayOnTop(w); + CLogMessage(w.data()).info(onTop ? + QStringLiteral("Window on top") : + QStringLiteral("Window not always on top")); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); Q_UNUSED(c); @@ -649,18 +664,22 @@ namespace BlackGui void CGuiApplication::addMenuHelp(QMenu &menu) { - QWidget *w = mainApplicationWidget(); + QPointer w = mainApplicationWidget(); if (!w) { return; } QAction *a = menu.addAction(w->style()->standardIcon(QStyle::SP_TitleBarContextHelpButton), "Online help"); - bool c = connect(a, &QAction::triggered, this, [this]() + QPointer myself(this); + + bool c = connect(a, &QAction::triggered, this, [ = ]() { - this->showHelp(); + if (!myself) { return; } + myself->showHelp(); }); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); a = menu.addAction(QApplication::windowIcon(), "About swift"); - c = connect(a, &QAction::triggered, this, [w]() + c = connect(a, &QAction::triggered, this, [ = ]() { + if (!w) { return; } CAboutDialog dialog(w); dialog.exec(); }); diff --git a/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp b/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp index 34b76a10c..36993dfc7 100644 --- a/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp +++ b/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp @@ -11,6 +11,7 @@ #include "simulatorxplane.h" #include +#include class QDBusConnection; @@ -28,8 +29,10 @@ namespace BlackSimPlugin void CXSwiftBusServiceProxy::getOwnAircraftSituationData(XPlaneData *o_xplaneData) { - std::function callback = [this, o_xplaneData](QDBusPendingCallWatcher * watcher) + QPointer myself(this); + std::function callback = [ = ](QDBusPendingCallWatcher * watcher) { + if (!myself) { return; } QDBusPendingReply reply = *watcher; if (!reply.isError()) {