Skip to content

Commit bf3ce97

Browse files
committed
LP-567 fix crash when connected and switching back from categorized mode
crash was due to highlight manager still referencing destroyed category tree items
1 parent 738171e commit bf3ce97

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

ground/gcs/src/plugins/uavobjectbrowser/treeitem.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ HighlightManager::HighlightManager()
4242
* Called to add item to list. Item is only added if absent.
4343
* Returns true if item was added, otherwise false.
4444
*/
45-
bool HighlightManager::add(TreeItem *itemToAdd)
45+
bool HighlightManager::add(TreeItem *item)
4646
{
4747
// Lock to ensure thread safety
4848
QMutexLocker locker(&m_mutex);
4949

5050
// Check so that the item isn't already in the list
51-
if (!m_items.contains(itemToAdd)) {
52-
m_items.insert(itemToAdd);
53-
emit updateHighlight(itemToAdd);
51+
if (!m_items.contains(item)) {
52+
m_items.insert(item);
53+
emit updateHighlight(item);
5454
return true;
5555
}
5656
return false;
@@ -60,20 +60,34 @@ bool HighlightManager::add(TreeItem *itemToAdd)
6060
* Called to remove item from list.
6161
* Returns true if item was removed, otherwise false.
6262
*/
63-
bool HighlightManager::remove(TreeItem *itemToRemove)
63+
bool HighlightManager::remove(TreeItem *item)
6464
{
6565
// Lock to ensure thread safety
6666
QMutexLocker locker(&m_mutex);
6767

6868
// Remove item and return result
69-
const bool removed = m_items.remove(itemToRemove);
69+
const bool removed = m_items.remove(item);
7070

7171
if (removed) {
72-
emit updateHighlight(itemToRemove);
72+
emit updateHighlight(item);
7373
}
7474
return removed;
7575
}
7676

77+
/*
78+
* Called to remove item from list.
79+
* Will not emit a signal. Called when destroying an item
80+
* Returns true if item was removed, otherwise false.
81+
*/
82+
bool HighlightManager::reset(TreeItem *item)
83+
{
84+
// Lock to ensure thread safety
85+
QMutexLocker locker(&m_mutex);
86+
87+
// Remove item and return result
88+
return m_items.remove(item);
89+
}
90+
7791
bool HighlightManager::startTimer(QTime expirationTime)
7892
{
7993
// Lock to ensure thread safety
@@ -163,6 +177,9 @@ TreeItem::TreeItem(const QVariant &data) :
163177

164178
TreeItem::~TreeItem()
165179
{
180+
if (m_highlightManager) {
181+
m_highlightManager->reset(this);
182+
}
166183
qDeleteAll(m_childItems);
167184
}
168185

@@ -273,7 +290,7 @@ void TreeItem::setHighlighted(bool highlighted, const QTime &ts)
273290
}
274291

275292
// If we have a parent, call recursively to update highlight status of parents.
276-
// This will ensure that the root of a leaf that is changed also is highlighted.
293+
// This will ensure that the root of a leaf that is changed is also highlighted.
277294
// Only updates that really changes values will trigger highlight of parents.
278295
if (m_parentItem) {
279296
m_parentItem->setHighlighted(highlighted, ts);

ground/gcs/src/plugins/uavobjectbrowser/treeitem.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ class HighlightManager : public QObject {
6262
public:
6363
HighlightManager();
6464

65-
// This is called when an item has been set to
66-
// highlighted = true.
67-
bool add(TreeItem *itemToAdd);
65+
// This is called when an item is set to highlighted = true.
66+
bool add(TreeItem *item);
6867

6968
// This is called when an item is set to highlighted = false;
70-
bool remove(TreeItem *itemToRemove);
69+
bool remove(TreeItem *item);
70+
71+
// This is called when an item is destroyed
72+
bool reset(TreeItem *item);
7173

7274
bool startTimer(QTime time);
7375

0 commit comments

Comments
 (0)