diff --git a/communication/communication.cpp b/communication/communication.cpp index 3610651..51956b6 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -156,14 +156,15 @@ void Communication::readTask() { std::lock_guard lk(messageCallbacksLock); - // We want callbacks to be able to access errors - EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); for(auto& cb : messageCallbacks) { if(!closing) { // We might have closed while reading or processing + // We want callbacks to be able to access errors + EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); cb.second.callIfMatch(msg); + EventManager::GetInstance().downgradeErrorsOnCurrentThread(); } } - EventManager::GetInstance().downgradeErrorsOnCurrentThread(); + } } } diff --git a/communication/multichannelcommunication.cpp b/communication/multichannelcommunication.cpp index ac71302..ac1b4e6 100644 --- a/communication/multichannelcommunication.cpp +++ b/communication/multichannelcommunication.cpp @@ -113,14 +113,14 @@ void MultiChannelCommunication::readTask() { continue; } - // We want callbacks to be able to access errors - EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); for(auto& cb : messageCallbacks) { // We might have closed while reading or processing if(!closing) { + // We want callbacks to be able to access errors + EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); cb.second.callIfMatch(msg); + EventManager::GetInstance().downgradeErrorsOnCurrentThread(); } } - EventManager::GetInstance().downgradeErrorsOnCurrentThread(); } } diff --git a/device/device.cpp b/device/device.cpp index f8f3b75..3632804 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -203,14 +203,14 @@ bool Device::close() { return false; } + if(isOnline()) + goOffline(); + if(internalHandlerCallbackID) com->removeMessageCallback(internalHandlerCallbackID); internalHandlerCallbackID = 0; - - if(isOnline()) - goOffline(); - + return com->close(); } diff --git a/include/icsneo/api/eventmanager.h b/include/icsneo/api/eventmanager.h index ebf9cef..d456cac 100644 --- a/include/icsneo/api/eventmanager.h +++ b/include/icsneo/api/eventmanager.h @@ -58,7 +58,7 @@ public: eventsLock.unlock(); runCallbacks(event); } else { - std::lock_guard lk(errorsMutex); + std::lock_guard errorsLock(errorsMutex); add_internal_error(event); } } else { diff --git a/platform/posix/stm32.cpp b/platform/posix/stm32.cpp index db38805..065b0f2 100644 --- a/platform/posix/stm32.cpp +++ b/platform/posix/stm32.cpp @@ -293,7 +293,6 @@ void STM32::readTask() { if(bytesRead > 0) readQueue.enqueue_bulk(readbuf, bytesRead); } - EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); } void STM32::writeTask() {