diff --git a/app/backend/computermanager.cpp b/app/backend/computermanager.cpp index 9c519d87..4f447502 100644 --- a/app/backend/computermanager.cpp +++ b/app/backend/computermanager.cpp @@ -319,12 +319,13 @@ void ComputerManager::startPolling() if (prefs.enableMdns) { // Start an MDNS query for GameStream hosts - m_MdnsBrowser = new QMdnsEngine::Browser(&m_MdnsServer, "_nvstream._tcp.local.", &m_MdnsCache); + m_MdnsServer.reset(new QMdnsEngine::Server()); + m_MdnsBrowser = new QMdnsEngine::Browser(m_MdnsServer.get(), "_nvstream._tcp.local."); connect(m_MdnsBrowser, &QMdnsEngine::Browser::serviceAdded, this, [this](const QMdnsEngine::Service& service) { qInfo() << "Discovered mDNS host:" << service.hostname(); - MdnsPendingComputer* pendingComputer = new MdnsPendingComputer(&m_MdnsServer, service); + MdnsPendingComputer* pendingComputer = new MdnsPendingComputer(m_MdnsServer, service); connect(pendingComputer, &MdnsPendingComputer::resolvedHost, this, &ComputerManager::handleMdnsServiceResolved); m_PendingResolution.append(pendingComputer); @@ -646,9 +647,10 @@ void ComputerManager::stopPollingAsync() m_PendingResolution.removeFirst(); } - // Delete the browser to stop discovery + // Delete the browser and server to stop discovery and refresh polling delete m_MdnsBrowser; m_MdnsBrowser = nullptr; + m_MdnsServer.reset(); // Interrupt all threads, but don't wait for them to terminate for (ComputerPollingEntry* entry : m_PollEntries) { diff --git a/app/backend/computermanager.h b/app/backend/computermanager.h index b8f45f40..011e094f 100644 --- a/app/backend/computermanager.h +++ b/app/backend/computermanager.h @@ -42,11 +42,11 @@ class MdnsPendingComputer : public QObject Q_OBJECT public: - explicit MdnsPendingComputer(QMdnsEngine::Server* server, + explicit MdnsPendingComputer(const QSharedPointer server, const QMdnsEngine::Service& service) : m_Hostname(service.hostname()), m_Port(service.port()), - m_Server(server), + m_ServerWeak(server), m_Resolver(nullptr) { // Start resolving @@ -94,8 +94,21 @@ signals: private: void resolve() { + // Delete our resolver, so we're guaranteed that nothing is referencing m_Server. delete m_Resolver; - m_Resolver = new QMdnsEngine::Resolver(m_Server, m_Hostname); + m_Resolver = nullptr; + + // Now delete our strong reference that we held on behalf of m_Resolver. + // The server may be destroyed after we make this call. + m_Server.reset(); + + // Re-acquire a strong reference if the server still exists. + m_Server = m_ServerWeak.toStrongRef(); + if (!m_Server) { + return; + } + + m_Resolver = new QMdnsEngine::Resolver(m_Server.get(), m_Hostname); connect(m_Resolver, &QMdnsEngine::Resolver::resolved, this, &MdnsPendingComputer::handleResolvedAddress); QTimer::singleShot(2000, this, &MdnsPendingComputer::handleResolvedTimeout); @@ -103,7 +116,8 @@ private: QByteArray m_Hostname; uint16_t m_Port; - QMdnsEngine::Server* m_Server; + QWeakPointer m_ServerWeak; + QSharedPointer m_Server; QMdnsEngine::Resolver* m_Resolver; QVector m_Addresses; }; @@ -245,9 +259,8 @@ private: QReadWriteLock m_Lock; QMap m_KnownHosts; QMap m_PollEntries; - QMdnsEngine::Server m_MdnsServer; + QSharedPointer m_MdnsServer; QMdnsEngine::Browser* m_MdnsBrowser; - QMdnsEngine::Cache m_MdnsCache; QVector m_PendingResolution; CompatFetcher m_CompatFetcher; DelayedFlushThread* m_DelayedFlushThread;