From aba1fb05dd342b18d4ac0d87ab5cf53854baed43 Mon Sep 17 00:00:00 2001 From: Gavin Hurlbut Date: Tue, 8 Feb 2011 20:45:22 -0800 Subject: [PATCH] Make it so the second frontend won't crash If for some reason, a user decides to run two frontends on the same box, and forget to set the service port to something non-standard for the second one, we want it to at least not crash. With this commit, it will just give up on running a webserver on the second run frontend, but it will run. Several tweaks were necessary to make the deleted webserver not double-free and not otherwise corrupt memory. --- mythtv/libs/libmythupnp/httpserver.cpp | 3 +-- mythtv/libs/libmythupnp/httpserver.h | 6 ++++-- mythtv/libs/libmythupnp/threadpool.cpp | 2 +- mythtv/libs/libmythupnp/threadpool.h | 7 +++++-- mythtv/programs/mythfrontend/main.cpp | 5 +++++ mythtv/programs/mythfrontend/mediarenderer.h | 1 + 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mythtv/libs/libmythupnp/httpserver.cpp b/mythtv/libs/libmythupnp/httpserver.cpp index c233a772d9d..71c363fa687 100644 --- a/mythtv/libs/libmythupnp/httpserver.cpp +++ b/mythtv/libs/libmythupnp/httpserver.cpp @@ -94,8 +94,7 @@ HttpServer::~HttpServer() { while (!m_extensions.empty()) { - delete m_extensions.back(); - m_extensions.pop_back(); + delete m_extensions.takeFirst(); } } diff --git a/mythtv/libs/libmythupnp/httpserver.h b/mythtv/libs/libmythupnp/httpserver.h index 0f3953ab42c..a4d3eaccdb3 100644 --- a/mythtv/libs/libmythupnp/httpserver.h +++ b/mythtv/libs/libmythupnp/httpserver.h @@ -57,8 +57,10 @@ class HttpServer; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// -class UPNP_PUBLIC HttpServerExtension +class UPNP_PUBLIC HttpServerExtension : public QObject { + Q_OBJECT + public: QString m_sName; @@ -77,7 +79,7 @@ class UPNP_PUBLIC HttpServerExtension // virtual bool Uninitialize ( ) = 0; }; -typedef QList HttpServerExtensionList; +typedef QList > HttpServerExtensionList; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// diff --git a/mythtv/libs/libmythupnp/threadpool.cpp b/mythtv/libs/libmythupnp/threadpool.cpp index 8badd1684ac..3d95b21bc20 100644 --- a/mythtv/libs/libmythupnp/threadpool.cpp +++ b/mythtv/libs/libmythupnp/threadpool.cpp @@ -405,7 +405,7 @@ WorkerThread *ThreadPool::GetWorkerThread() WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout ) { - QString sName = m_sName + "_WorkerThread"; + QString sName = QString(m_sName + "_WorkerThread"); long nThreadCount; VERBOSE( VB_UPNP, QString( "ThreadPool:AddWorkerThread - %1" ).arg( sName )); diff --git a/mythtv/libs/libmythupnp/threadpool.h b/mythtv/libs/libmythupnp/threadpool.h index 732fcb0cfe8..857aa661af9 100644 --- a/mythtv/libs/libmythupnp/threadpool.h +++ b/mythtv/libs/libmythupnp/threadpool.h @@ -33,6 +33,7 @@ using namespace std; #include #include #include +#include class ThreadPool; @@ -107,7 +108,7 @@ class WorkerThread : public QThread CEvent m_Initialized; bool m_bInitialized; - ThreadPool *m_pThreadPool; + QPointer m_pThreadPool; volatile bool m_bTermRequested; QString m_sName; @@ -148,8 +149,10 @@ class WorkerThread : public QThread typedef deque WorkerThreadList; -class ThreadPool +class ThreadPool : public QObject { + Q_OBJECT + friend class WorkerThread; protected: diff --git a/mythtv/programs/mythfrontend/main.cpp b/mythtv/programs/mythfrontend/main.cpp index b04c50ac30d..504105d1773 100644 --- a/mythtv/programs/mythfrontend/main.cpp +++ b/mythtv/programs/mythfrontend/main.cpp @@ -1273,6 +1273,11 @@ int main(int argc, char **argv) gContext = new MythContext(MYTH_BINARY_VERSION); g_pUPnp = new MediaRenderer(); + if (!g_pUPnp->initialized()) + { + delete g_pUPnp; + g_pUPnp = NULL; + } // Override settings as early as possible to cover bootstrapped screens // such as the language prompt diff --git a/mythtv/programs/mythfrontend/mediarenderer.h b/mythtv/programs/mythfrontend/mediarenderer.h index 6e117e39004..9176192bfc6 100644 --- a/mythtv/programs/mythfrontend/mediarenderer.h +++ b/mythtv/programs/mythfrontend/mediarenderer.h @@ -44,6 +44,7 @@ class MediaRenderer : public UPnp DeviceLocation *GetDefaultMaster(); void SetDefaultMaster( DeviceLocation *pDeviceLoc, const QString &sPin ); + bool initialized() { return (m_pHttpServer != NULL); } };