From 77cbc36ad5002a315b250d860cc7491d60030478 Mon Sep 17 00:00:00 2001 From: pip Date: Sat, 26 Feb 2011 12:56:14 +0000 Subject: [PATCH] Added object destroyed monitoring to the RawHIDEnumerationThread object to ensure it doesn't crash if it's parent (m_rawhid) is destroyed. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2888 ebee16cc-31ac-478f-84a7-5cbb03baadba --- .../src/plugins/rawhid/rawhidplugin.cpp | 19 ++++++++++++++++++- .../src/plugins/rawhid/rawhidplugin.h | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp index 6920937ac..b9190c9c6 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp @@ -36,12 +36,14 @@ #include "rawhid_const.h" +// *************************************** RawHIDEnumerationThread::RawHIDEnumerationThread(RawHIDConnection *rawhid) : QThread(rawhid), // Pip m_rawhid(rawhid), m_running(true) { + connect(m_rawhid, SLOT(destroyed(QObject *)), this, SLOT(onRawHidConnectionDestroyed(QObject *))); // Pip } RawHIDEnumerationThread::~RawHIDEnumerationThread() @@ -53,6 +55,16 @@ RawHIDEnumerationThread::~RawHIDEnumerationThread() qDebug() << "Cannot terminate RawHIDEnumerationThread"; } +void RawHIDEnumerationThread::onRawHidConnectionDestroyed(QObject *obj) // Pip +{ + QMutexLocker locker(&mutex); + + if (!m_rawhid || m_rawhid != obj) + return; + + m_rawhid = NULL; +} + void RawHIDEnumerationThread::run() { QStringList devices = m_rawhid->availableDevices(); @@ -61,8 +73,10 @@ void RawHIDEnumerationThread::run() while (m_running) { + mutex.lock(); // Pip + // update available devices every second (doesn't need more) - if (++counter >= 100 && !m_rawhid->deviceOpened()) + if (++counter >= 100 && m_rawhid && !m_rawhid->deviceOpened()) { counter = 0; @@ -74,10 +88,13 @@ void RawHIDEnumerationThread::run() } } + mutex.unlock(); // Pip + msleep(10); } } +// *************************************** RawHIDConnection::RawHIDConnection() : m_enumerateThread(this) diff --git a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.h b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.h index a285b8878..b3cf7e7a8 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.h +++ b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.h @@ -58,9 +58,15 @@ public: signals: void enumerationChanged(); +protected slots: + void onRawHidConnectionDestroyed(QObject *obj); // Pip + protected: RawHIDConnection *m_rawhid; bool m_running; + +private: + QMutex mutex; };