From a327104ed791f607d703e66744fd07b5365fcc31 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 01:36:05 +0000 Subject: [PATCH] la_mojo nodes and modules: register for events only when fully built A handler was registered in the basicEventHandler constructor (before the derived members existed) and removed in its destructor (after they were destroyed): the event heap could call dispatchEvent() on a half- built or half-destroyed node or module when one was created or deleted while la_mojo runs (node / module reload). - Camera nodes (deviceNodePTZCamControl, deviceNodePresetCamControl, deviceNodeStaticCamControl): basicEventHandler(false), attachEvents() at the end of both constructors, detachEvents() first thing in the destructor. - Kernel modules: kernelModule no longer registers in its constructor; kernelModuleFactory attaches a module right after creating it (all modules are created there), before startService(). Every module destructor (USB relay, Kafka, FlyHunter, SSDP, ONVIF observer, embed video (both variants), modem manager, Bosch, attention cloud, report backupper) calls detachEvents() first. - basicEventHeap::moveHandlerTop/Bottom no longer register a handler that is not registered (would have registered an object twice once attachEvents() runs). Sensor nodes are not event handlers and are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodePTZCamControl.cpp | 9 +++++++-- lib/App/common/deviceNodePresetCamControl.cpp | 9 +++++++-- lib/App/common/deviceNodeStaticCamControl.cpp | 9 +++++++-- lib/App/common/kernelModule.cpp | 5 ++++- lib/App/common/kernelModuleAttentionCloud.cpp | 1 + lib/App/common/kernelModuleBosch.cpp | 1 + lib/App/common/kernelModuleEmbedVideo.cpp | 2 ++ lib/App/common/kernelModuleFactory.cpp | 2 ++ lib/App/common/kernelModuleFlyHunter.cpp | 1 + lib/App/common/kernelModuleKafka.cpp | 1 + lib/App/common/kernelModuleModemManager.cpp | 1 + lib/App/common/kernelModuleOnvifObserver.cpp | 1 + lib/App/common/kernelModuleReportBackupper.cpp | 4 +++- lib/App/common/kernelModuleSSDP.cpp | 1 + lib/App/common/kernelModuleUSBRelay.cpp | 4 +++- lib/components/base/basicEventHeap.cpp | 10 ++++++---- 16 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib/App/common/deviceNodePTZCamControl.cpp b/lib/App/common/deviceNodePTZCamControl.cpp index fbfd12c2..0657a411 100644 --- a/lib/App/common/deviceNodePTZCamControl.cpp +++ b/lib/App/common/deviceNodePTZCamControl.cpp @@ -6,22 +6,27 @@ deviceNodePTZCamControl::deviceNodePTZCamControl(DBC_NODE_DATA * Data) : deviceNodeCamera(Data), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodePTZCamControl::deviceNodePTZCamControl(uint32_t id) : deviceNodeCamera(id), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { m_sMediaSource.mediaSourceId = m_sNodeData.aux_id; strncpy(m_sMediaSource.mediaName, m_sNodeData.name, 128); m_sMediaSource.mediaName[127] = (char) 0; memset(&m_sCtrlData, 0, sizeof(CAMERA_CTRL_DATA)); + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodePTZCamControl::~deviceNodePTZCamControl() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) stopService(); } diff --git a/lib/App/common/deviceNodePresetCamControl.cpp b/lib/App/common/deviceNodePresetCamControl.cpp index fd4d1c24..9543dec6 100644 --- a/lib/App/common/deviceNodePresetCamControl.cpp +++ b/lib/App/common/deviceNodePresetCamControl.cpp @@ -6,22 +6,27 @@ deviceNodePresetCamControl::deviceNodePresetCamControl(DBC_NODE_DATA * Data) : deviceNodeCamera(Data), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodePresetCamControl::deviceNodePresetCamControl(uint32_t id) : deviceNodeCamera(id), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { m_sMediaSource.mediaSourceId = m_sNodeData.aux_id; strncpy(m_sMediaSource.mediaName, m_sNodeData.name, 128); m_sMediaSource.mediaName[127] = (char) 0; memset(&m_sCtrlData, 0, sizeof(CAMERA_CTRL_DATA)); + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodePresetCamControl::~deviceNodePresetCamControl() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) stopService(); } diff --git a/lib/App/common/deviceNodeStaticCamControl.cpp b/lib/App/common/deviceNodeStaticCamControl.cpp index 1e8f239d..46cbe480 100644 --- a/lib/App/common/deviceNodeStaticCamControl.cpp +++ b/lib/App/common/deviceNodeStaticCamControl.cpp @@ -5,7 +5,7 @@ deviceNodeStaticCamControl::deviceNodeStaticCamControl(DBC_NODE_DATA * Data) : deviceNodeCamera(Data), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { // m_pLog = basicLogCollection::getInstance(); /* @@ -39,20 +39,25 @@ deviceNodeStaticCamControl::deviceNodeStaticCamControl(DBC_NODE_DATA * Data) : #endif m_pPosition = new basicPosition( m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw ); */ + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodeStaticCamControl::deviceNodeStaticCamControl(uint32_t id) : deviceNodeCamera(id), - basicEventHandler() + basicEventHandler(false) // registered at the end of the constructor { m_sMediaSource.mediaSourceId = m_sNodeData.aux_id; strncpy(m_sMediaSource.mediaName, m_sNodeData.name, 128); m_sMediaSource.mediaName[127] = (char) 0; memset(&m_sCtrlData, 0, sizeof(CAMERA_CTRL_DATA)); + // Receive events only now that the object is completely constructed + attachEvents(); } deviceNodeStaticCamControl::~deviceNodeStaticCamControl() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) LOG("Destructor"); stopService(); } diff --git a/lib/App/common/kernelModule.cpp b/lib/App/common/kernelModule.cpp index abc3a645..bbf2d3b5 100644 --- a/lib/App/common/kernelModule.cpp +++ b/lib/App/common/kernelModule.cpp @@ -6,8 +6,11 @@ #include "SQLite_dataBinder.h" #endif +// Not registered for events here: the derived module's members are not built yet. The module factory +// calls attachEvents() once the module is constructed; each module calls detachEvents() first thing in +// its destructor. kernelModule::kernelModule(uint32_t id, KERNEL_MODULE_TYPE type) : - basicEventHandler(), + basicEventHandler(false), basicEngine() { m_pLog = basicLogCollection::getInstance(); diff --git a/lib/App/common/kernelModuleAttentionCloud.cpp b/lib/App/common/kernelModuleAttentionCloud.cpp index e9e762b8..6ab60e63 100644 --- a/lib/App/common/kernelModuleAttentionCloud.cpp +++ b/lib/App/common/kernelModuleAttentionCloud.cpp @@ -11,6 +11,7 @@ kernelAttentionCloud::kernelAttentionCloud( uint32_t id ): kernelModule( id , KE }; kernelAttentionCloud::~kernelAttentionCloud(){ + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) stopService( true ); waitThreadEnd( true ); }; diff --git a/lib/App/common/kernelModuleBosch.cpp b/lib/App/common/kernelModuleBosch.cpp index c6335f37..533b0e15 100644 --- a/lib/App/common/kernelModuleBosch.cpp +++ b/lib/App/common/kernelModuleBosch.cpp @@ -26,6 +26,7 @@ kernelModuleBosch::kernelModuleBosch(uint32_t ModuleID) : } kernelModuleBosch::~kernelModuleBosch(){ + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) m_bEnabled = false; if ( m_pCURL ){ curl_easy_cleanup(m_pCURL); diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 1e70334e..f6f4af18 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -186,6 +186,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) : kernelModuleEmbedVideo::~kernelModuleEmbedVideo() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) #ifdef DEBUG LOG("Destructor"); #endif @@ -2436,6 +2437,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) : kernelModuleEmbedVideo::~kernelModuleEmbedVideo() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) } diff --git a/lib/App/common/kernelModuleFactory.cpp b/lib/App/common/kernelModuleFactory.cpp index ec9c7485..cb615022 100644 --- a/lib/App/common/kernelModuleFactory.cpp +++ b/lib/App/common/kernelModuleFactory.cpp @@ -249,6 +249,8 @@ kernelModule * kernelModuleFactory::createModule(int id) }; if (tVal != NULL) { + // Receive events only now that the module object is completely constructed + tVal->attachEvents(); if (tVal->startService()) { m_mModulesProtector.lock(); diff --git a/lib/App/common/kernelModuleFlyHunter.cpp b/lib/App/common/kernelModuleFlyHunter.cpp index c04d1496..22aa3643 100644 --- a/lib/App/common/kernelModuleFlyHunter.cpp +++ b/lib/App/common/kernelModuleFlyHunter.cpp @@ -30,6 +30,7 @@ kernelModuleFlyHunter::kernelModuleFlyHunter(uint32_t ModuleID) : kernelModuleFlyHunter::~kernelModuleFlyHunter() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) m_bEnabled = false; waitThreadEnd(true); #ifndef SIMULATE_FH diff --git a/lib/App/common/kernelModuleKafka.cpp b/lib/App/common/kernelModuleKafka.cpp index 71993cc0..855d4e43 100644 --- a/lib/App/common/kernelModuleKafka.cpp +++ b/lib/App/common/kernelModuleKafka.cpp @@ -98,6 +98,7 @@ kernelModuleKafka::kernelModuleKafka(uint32_t ModuleID) : kernelModuleKafka::~kernelModuleKafka() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) m_bEnabled = false; diff --git a/lib/App/common/kernelModuleModemManager.cpp b/lib/App/common/kernelModuleModemManager.cpp index 5d175bcb..219e13b1 100644 --- a/lib/App/common/kernelModuleModemManager.cpp +++ b/lib/App/common/kernelModuleModemManager.cpp @@ -55,6 +55,7 @@ kernelModuleModemManager::kernelModuleModemManager(uint32_t moduleID) : kernelModuleModemManager::~kernelModuleModemManager() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) // if ( m_vUserData.size() ) { // for ( VECTOR_MDM_USER_DATA::iterator idx = m_vUserData.begin(); idx < m_vUserData.end(); idx++ ) { // if ( !(*idx)->finished ) pthread_join( (*idx)->handler, NULL); diff --git a/lib/App/common/kernelModuleOnvifObserver.cpp b/lib/App/common/kernelModuleOnvifObserver.cpp index d2cf338e..d9780430 100644 --- a/lib/App/common/kernelModuleOnvifObserver.cpp +++ b/lib/App/common/kernelModuleOnvifObserver.cpp @@ -105,6 +105,7 @@ kernelModuleOnvifObserver::kernelModuleOnvifObserver(uint32_t ModuleID) : kernelModuleOnvifObserver::~kernelModuleOnvifObserver() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) if (! m_bDetached) { stopService(true); diff --git a/lib/App/common/kernelModuleReportBackupper.cpp b/lib/App/common/kernelModuleReportBackupper.cpp index 3c130e9d..62484d1a 100644 --- a/lib/App/common/kernelModuleReportBackupper.cpp +++ b/lib/App/common/kernelModuleReportBackupper.cpp @@ -30,7 +30,9 @@ kernelModuleReportBackupper::kernelModuleReportBackupper(uint32_t moduleID) : m_bBackupIsWorking = false; } -kernelModuleReportBackupper::~kernelModuleReportBackupper() {} +kernelModuleReportBackupper::~kernelModuleReportBackupper() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) +} bool kernelModuleReportBackupper::validate() { diff --git a/lib/App/common/kernelModuleSSDP.cpp b/lib/App/common/kernelModuleSSDP.cpp index 85282727..b43c1195 100644 --- a/lib/App/common/kernelModuleSSDP.cpp +++ b/lib/App/common/kernelModuleSSDP.cpp @@ -98,6 +98,7 @@ kernelModuleSSDPAnnouncer::kernelModuleSSDPAnnouncer(uint32_t ModuleID) : kernelModuleSSDPAnnouncer::~kernelModuleSSDPAnnouncer() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) if (m_iSocketFID != -1) { close_socket(); diff --git a/lib/App/common/kernelModuleUSBRelay.cpp b/lib/App/common/kernelModuleUSBRelay.cpp index 8d1d0a62..bf159eff 100644 --- a/lib/App/common/kernelModuleUSBRelay.cpp +++ b/lib/App/common/kernelModuleUSBRelay.cpp @@ -307,7 +307,9 @@ kernelModuleUSBRelay::kernelModuleUSBRelay(uint32_t ModuleID) : { } -kernelModuleUSBRelay::~kernelModuleUSBRelay() {} +kernelModuleUSBRelay::~kernelModuleUSBRelay() { + detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h) +} bool kernelModuleUSBRelay::dispatchEvent(EVENT_CONTAINER * Evt) { diff --git a/lib/components/base/basicEventHeap.cpp b/lib/components/base/basicEventHeap.cpp index 639c4d3a..f757fadf 100644 --- a/lib/components/base/basicEventHeap.cpp +++ b/lib/components/base/basicEventHeap.cpp @@ -127,10 +127,11 @@ void basicEventHeap::moveHandlerBottom(basicEventHandler * handler) { std::lock_guard tLock(m_mHandlers); V_EVT_HANDLER::iterator idx = std::find(m_vHandlers.begin(), m_vHandlers.end(), handler); - if (idx != m_vHandlers.end()) + if (idx == m_vHandlers.end()) { - m_vHandlers.erase(idx); + return; // not registered (not attached yet / detached): moving must not register it }; + m_vHandlers.erase(idx); m_vHandlers.push_back(handler); } @@ -138,10 +139,11 @@ void basicEventHeap::moveHandlerTop(basicEventHandler * handler) { std::lock_guard tLock(m_mHandlers); V_EVT_HANDLER::iterator idx = std::find(m_vHandlers.begin(), m_vHandlers.end(), handler); - if (idx != m_vHandlers.end()) + if (idx == m_vHandlers.end()) { - m_vHandlers.erase(idx); + return; // not registered (not attached yet / detached): moving must not register it }; + m_vHandlers.erase(idx); m_vHandlers.insert(m_vHandlers.begin(), handler); } -- 2.39.5