From 5fe34d0b1f2e54014c148d37a1cf325b69cbf3d6 Mon Sep 17 00:00:00 2001 From: dogmaphobic Date: Fri, 20 Feb 2015 01:26:27 -0500 Subject: [PATCH] Cleaning dangling links --- src/comm/LinkInterface.h | 4 +++- src/comm/LinkManager.cc | 25 +++++++++++++++++++++---- src/comm/LinkManager.h | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/comm/LinkInterface.h b/src/comm/LinkInterface.h index aa5526d14..ac0cd704e 100644 --- a/src/comm/LinkInterface.h +++ b/src/comm/LinkInterface.h @@ -57,7 +57,8 @@ public: LinkInterface() : QThread(0), _ownedByLinkManager(false), - _deletedByLinkManager(false) + _deletedByLinkManager(false), + _flaggedForDeletion(false) { // Initialize everything for the data rate calculation buffers. inDataIndex = 0; @@ -340,6 +341,7 @@ private: bool _ownedByLinkManager; ///< true: This link has been added to LinkManager, false: Link not added to LinkManager bool _deletedByLinkManager; ///< true: Link being deleted from LinkManager, false: error, Links should only be deleted from LinkManager + bool _flaggedForDeletion; ///< true: Garbage colletion ready }; #endif // _LINKINTERFACE_H_ diff --git a/src/comm/LinkManager.cc b/src/comm/LinkManager.cc index 3fffbdfd8..f8ba9d11f 100644 --- a/src/comm/LinkManager.cc +++ b/src/comm/LinkManager.cc @@ -193,20 +193,37 @@ bool LinkManager::disconnectLink(LinkInterface* link) { Q_ASSERT(link); if (link->_disconnect()) { - // TODO There is no point in disconnecting it if will stay around. - // This should be turned into a delete link instead. Deleting a link - // is not yet possible as LinkManager is broken. - // Disconnect this link from its configuration LinkConfiguration* config = link->getLinkConfiguration(); if(config) { config->setLink(NULL); } + // Link is now done and over with. We can't yet delete it because it + // takes a while for the MAVLink protocol to take notice of it. We + // flag it for delayed deletion for final clean up. + link->_flaggedForDeletion = true; + QTimer::singleShot(1000, this, &LinkManager::_delayedDeleteLink); return true; } else { return false; } } +void LinkManager::_delayedDeleteLink() +{ + _linkListMutex.lock(); + foreach (LinkInterface* link, _links) + { + Q_ASSERT(link); + if (link->_flaggedForDeletion) { + qDebug() << "Link deleted: " << link->getName(); + _linkListMutex.unlock(); + deleteLink(link); + return; + } + } + _linkListMutex.unlock(); +} + void LinkManager::deleteLink(LinkInterface* link) { Q_ASSERT(link); diff --git a/src/comm/LinkManager.h b/src/comm/LinkManager.h index ba0e057f0..1553f07a6 100644 --- a/src/comm/LinkManager.h +++ b/src/comm/LinkManager.h @@ -141,6 +141,7 @@ signals: private slots: void _linkConnected(void); void _linkDisconnected(void); + void _delayedDeleteLink(); private: /// All access to LinkManager is through LinkManager::instance -- 2.22.0