From 00fc81405bbf88fe3594d7d1898a793139c5971c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 14 Apr 2025 12:02:15 +0200 Subject: [PATCH] NetworkDiscovery: Fix possible crash on monitor cleanup DateTime: Improve performance by using static current QDateTime method --- .../network/networkdevicediscoveryimpl.cpp | 18 +++++++++++------- .../network/networkdevicediscoveryimpl.h | 2 ++ libnymea-core/jsonrpc/systemhandler.cpp | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libnymea-core/hardware/network/networkdevicediscoveryimpl.cpp b/libnymea-core/hardware/network/networkdevicediscoveryimpl.cpp index 38545742..ada5477b 100644 --- a/libnymea-core/hardware/network/networkdevicediscoveryimpl.cpp +++ b/libnymea-core/hardware/network/networkdevicediscoveryimpl.cpp @@ -263,7 +263,7 @@ NetworkDeviceMonitor *NetworkDeviceDiscoveryImpl::registerMonitor(Thing *thing) } else { // Create a new monitor for the internal use internalMonitor = new NetworkDeviceMonitorImpl(macAddress, hostName, address, this); - m_monitors.insert(internalMonitor, QVector()); + m_monitors.insert(internalMonitor, QVector()); } internalMonitor->setMonitorMode(mode); @@ -287,14 +287,14 @@ NetworkDeviceMonitor *NetworkDeviceDiscoveryImpl::registerMonitor(Thing *thing) } break; case NetworkDeviceInfo::MonitorModeHostName: - // Search the unique mac address + // Search the hostname in the cache if (networkDeviceInfo.hostName() == internalMonitor->hostName()) { qCDebug(dcNetworkDeviceDiscovery()) << "Host name monitor:" << networkDeviceInfo; internalMonitor->setNetworkDeviceInfo(networkDeviceInfo); } break; case NetworkDeviceInfo::MonitorModeIp: - // Search the unique mac address + // Search the IP in the cache if (networkDeviceInfo.address() == internalMonitor->address()) { qCDebug(dcNetworkDeviceDiscovery()) << "IP monitor:" << networkDeviceInfo; internalMonitor->setNetworkDeviceInfo(networkDeviceInfo); @@ -857,7 +857,7 @@ void NetworkDeviceDiscoveryImpl::processArpTraffic(const QNetworkInterface &inte bool NetworkDeviceDiscoveryImpl::longerAgoThan(const QDateTime &dateTime, uint seconds) { - uint duration = (QDateTime::currentDateTime().toMSecsSinceEpoch() - dateTime.toMSecsSinceEpoch()) / 1000.0; + uint duration = (QDateTime::currentMSecsSinceEpoch() - dateTime.toMSecsSinceEpoch()) / 1000.0; return duration >= seconds; } @@ -905,9 +905,7 @@ NetworkDeviceMonitorImpl *NetworkDeviceDiscoveryImpl::createPluginMonitor(Networ }); // In case the plugin user is deleting the monitor object, we need to clean up here and check if we can remove the internal monitor - connect(pluginMonitor, &NetworkDeviceDiscoveryImpl::destroyed, this, [this, pluginMonitor](QObject *) { - cleanupPluginMonitor(pluginMonitor); - }); + connect(pluginMonitor, &NetworkDeviceMonitorImpl::destroyed, this, &NetworkDeviceDiscoveryImpl::onPluginMonitorDeleted); return pluginMonitor; } @@ -918,6 +916,7 @@ void NetworkDeviceDiscoveryImpl::cleanupPluginMonitor(NetworkDeviceMonitorImpl * foreach (NetworkDeviceMonitorImpl *internalMonitor, m_monitors.keys()) { if (m_monitors.value(internalMonitor).contains(pluginMonitor)) { m_monitors[internalMonitor].removeAll(pluginMonitor); + disconnect(pluginMonitor, &NetworkDeviceMonitorImpl::destroyed, this, &NetworkDeviceDiscoveryImpl::onPluginMonitorDeleted); pluginMonitor->deleteLater(); if (m_monitors.value(internalMonitor).isEmpty()) { @@ -1006,4 +1005,9 @@ void NetworkDeviceDiscoveryImpl::finishDiscovery() } } +void NetworkDeviceDiscoveryImpl::onPluginMonitorDeleted(QObject *) +{ + cleanupPluginMonitor(qobject_cast(sender())); +} + } diff --git a/libnymea-core/hardware/network/networkdevicediscoveryimpl.h b/libnymea-core/hardware/network/networkdevicediscoveryimpl.h index f17f04c6..339fda87 100644 --- a/libnymea-core/hardware/network/networkdevicediscoveryimpl.h +++ b/libnymea-core/hardware/network/networkdevicediscoveryimpl.h @@ -154,6 +154,8 @@ private slots: void evaluateMonitors(); void finishDiscovery(); + void onPluginMonitorDeleted(QObject *); + }; } diff --git a/libnymea-core/jsonrpc/systemhandler.cpp b/libnymea-core/jsonrpc/systemhandler.cpp index a5dfbdd4..48bf3a55 100644 --- a/libnymea-core/jsonrpc/systemhandler.cpp +++ b/libnymea-core/jsonrpc/systemhandler.cpp @@ -270,7 +270,7 @@ SystemHandler::SystemHandler(Platform *platform, QObject *parent): }); connect(m_platform->systemController(), &PlatformSystemController::timeConfigurationChanged, this, [this](){ QVariantMap params; - params.insert("time", QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000); + params.insert("time", QDateTime::currentMSecsSinceEpoch() / 1000); params.insert("timeZone", QTimeZone::systemTimeZoneId()); params.insert("automaticTimeAvailable", m_platform->systemController()->automaticTimeAvailable()); params.insert("automaticTime", m_platform->systemController()->automaticTime()); @@ -401,7 +401,7 @@ JsonReply *SystemHandler::GetTime(const QVariantMap ¶ms) const QVariantMap returns; returns.insert("automaticTimeAvailable", m_platform->systemController()->automaticTimeAvailable()); returns.insert("automaticTime", m_platform->systemController()->automaticTime()); - returns.insert("time", QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000); + returns.insert("time", QDateTime::currentMSecsSinceEpoch() / 1000); returns.insert("timeZone", QTimeZone::systemTimeZoneId()); return createReply(returns); }