From 4e915e0b5e1883762f1a59b7df486056b4e7bf10 Mon Sep 17 00:00:00 2001
From: Gavin Hurlbut <ghurlbut@mythtv.org>
Date: Thu, 3 Feb 2011 22:22:04 -0800
Subject: [PATCH] Change to using QPointer so double delete issues stop

We keep on seem to get some race conditions with myth_system launched things
exiting too fast (i.e. command not found, fast crashes) and causing a set of
race conditions involving deleting the MythSystemUnix item when the process
ends.  The use of QPointer should avoid these problems as when a QPointer item
is deleted, the reference is automatically zeroed, even if it was deleted
elsewhere (or so the docs seem to say).  With that plus a few more null checks
and we can avoid using it after it's freed.
---
 mythtv/libs/libmythbase/system-unix.cpp |    6 ++++++
 mythtv/libs/libmythbase/system-unix.h   |    5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/mythtv/libs/libmythbase/system-unix.cpp b/mythtv/libs/libmythbase/system-unix.cpp
index 2c43a4c..9b64d08 100644
--- a/mythtv/libs/libmythbase/system-unix.cpp
+++ b/mythtv/libs/libmythbase/system-unix.cpp
@@ -335,6 +335,8 @@ void MythSystemManager::run(void)
             next = i + 1;
             pid  = i.key();
             ms   = i.value();
+            if (!ms)
+                continue;
 
             // handle processes beyond marked timeout
             if( ms->m_timeout > 0 && ms->m_timeout < now )
@@ -443,6 +445,10 @@ void MythSystemSignalManager::run(void)
             MythSystemUnix *ms = msList.takeFirst();
             listLock.unlock();
 
+            // This can happen if it has been deleted already
+            if (!ms)
+                continue;
+
             ms->m_parent->HandlePostRun();
 
             if (ms->m_stdpipe[0] > 0)
diff --git a/mythtv/libs/libmythbase/system-unix.h b/mythtv/libs/libmythbase/system-unix.h
index 0785559..7279811 100644
--- a/mythtv/libs/libmythbase/system-unix.h
+++ b/mythtv/libs/libmythbase/system-unix.h
@@ -10,14 +10,15 @@
 #include <QThread>
 #include <QWaitCondition>
 #include <QMutex>
+#include <QPointer>
 #include <sys/select.h>
 #include "mythsystem.h"
 
 class MythSystemUnix;
 
-typedef QMap<pid_t, MythSystemUnix *> MSMap_t;
+typedef QMap<pid_t, QPointer<MythSystemUnix> > MSMap_t;
 typedef QMap<int, QBuffer *> PMap_t;
-typedef QList<MythSystemUnix *> MSList_t;
+typedef QList<QPointer<MythSystemUnix> > MSList_t;
 
 class MythSystemIOHandler: public QThread
 {
-- 
1.7.0.4

