1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-18 08:54:15 +01:00

LP-567 fix crash when connected and switching back from categorized mode

crash was due to highlight manager still referencing destroyed category tree items
This commit is contained in:
Philippe Renon 2018-03-26 22:22:42 +02:00
parent 738171e4a6
commit bf3ce97e32
2 changed files with 31 additions and 12 deletions

View File

@ -42,15 +42,15 @@ HighlightManager::HighlightManager()
* Called to add item to list. Item is only added if absent. * Called to add item to list. Item is only added if absent.
* Returns true if item was added, otherwise false. * Returns true if item was added, otherwise false.
*/ */
bool HighlightManager::add(TreeItem *itemToAdd) bool HighlightManager::add(TreeItem *item)
{ {
// Lock to ensure thread safety // Lock to ensure thread safety
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
// Check so that the item isn't already in the list // Check so that the item isn't already in the list
if (!m_items.contains(itemToAdd)) { if (!m_items.contains(item)) {
m_items.insert(itemToAdd); m_items.insert(item);
emit updateHighlight(itemToAdd); emit updateHighlight(item);
return true; return true;
} }
return false; return false;
@ -60,20 +60,34 @@ bool HighlightManager::add(TreeItem *itemToAdd)
* Called to remove item from list. * Called to remove item from list.
* Returns true if item was removed, otherwise false. * Returns true if item was removed, otherwise false.
*/ */
bool HighlightManager::remove(TreeItem *itemToRemove) bool HighlightManager::remove(TreeItem *item)
{ {
// Lock to ensure thread safety // Lock to ensure thread safety
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
// Remove item and return result // Remove item and return result
const bool removed = m_items.remove(itemToRemove); const bool removed = m_items.remove(item);
if (removed) { if (removed) {
emit updateHighlight(itemToRemove); emit updateHighlight(item);
} }
return removed; return removed;
} }
/*
* Called to remove item from list.
* Will not emit a signal. Called when destroying an item
* Returns true if item was removed, otherwise false.
*/
bool HighlightManager::reset(TreeItem *item)
{
// Lock to ensure thread safety
QMutexLocker locker(&m_mutex);
// Remove item and return result
return m_items.remove(item);
}
bool HighlightManager::startTimer(QTime expirationTime) bool HighlightManager::startTimer(QTime expirationTime)
{ {
// Lock to ensure thread safety // Lock to ensure thread safety
@ -163,6 +177,9 @@ TreeItem::TreeItem(const QVariant &data) :
TreeItem::~TreeItem() TreeItem::~TreeItem()
{ {
if (m_highlightManager) {
m_highlightManager->reset(this);
}
qDeleteAll(m_childItems); qDeleteAll(m_childItems);
} }
@ -273,7 +290,7 @@ void TreeItem::setHighlighted(bool highlighted, const QTime &ts)
} }
// If we have a parent, call recursively to update highlight status of parents. // If we have a parent, call recursively to update highlight status of parents.
// This will ensure that the root of a leaf that is changed also is highlighted. // This will ensure that the root of a leaf that is changed is also highlighted.
// Only updates that really changes values will trigger highlight of parents. // Only updates that really changes values will trigger highlight of parents.
if (m_parentItem) { if (m_parentItem) {
m_parentItem->setHighlighted(highlighted, ts); m_parentItem->setHighlighted(highlighted, ts);

View File

@ -62,12 +62,14 @@ class HighlightManager : public QObject {
public: public:
HighlightManager(); HighlightManager();
// This is called when an item has been set to // This is called when an item is set to highlighted = true.
// highlighted = true. bool add(TreeItem *item);
bool add(TreeItem *itemToAdd);
// This is called when an item is set to highlighted = false; // This is called when an item is set to highlighted = false;
bool remove(TreeItem *itemToRemove); bool remove(TreeItem *item);
// This is called when an item is destroyed
bool reset(TreeItem *item);
bool startTimer(QTime time); bool startTimer(QTime time);