From 13a8a50d52b0d41e63a35064ea33141a9888c1f8 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Fri, 21 Mar 2025 13:42:19 +0100 Subject: [PATCH] MT#59962 DSMChartReader: add dl handler cleanup Cleanup DL handlers when cleaning up DSMChartReader. Change-Id: I54b21a0685cb4d6a5568bf97243949476132d90e --- apps/dsm/DSM.cpp | 27 +++++++++++++++++-- apps/dsm/DSMChartReader.cpp | 54 ++++++++++++++++++++++++++++--------- apps/dsm/DSMChartReader.h | 10 ++++++- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/apps/dsm/DSM.cpp b/apps/dsm/DSM.cpp index 785ade87..2ebad9db 100644 --- a/apps/dsm/DSM.cpp +++ b/apps/dsm/DSM.cpp @@ -876,7 +876,18 @@ int DSMFactory::preloadModules(AmConfigReader& cfg, string& res, const string& M res = "importing module '"+*it+"' for preload\n"; return -1; } - DSMModule* last_loaded = preload_reader.mods.back(); + + DSMModule* last_loaded = NULL; + modLinkHdl modHdl = preload_reader.mods.back(); + + if (modHdl.mod) + last_loaded = modHdl.mod; + else + { + res = "Error while preloading '"+*it+"'\n"; + return -1; + } + if (last_loaded) { if (last_loaded->preload()) { res = "Error while preloading '"+*it+"'\n"; @@ -919,7 +930,19 @@ void DSMFactory::preloadModule(const AmArg& args, AmArg& ret) { ret.push("importing module '"+mod_name+"' for preload"); return; } - DSMModule* last_loaded = preload_reader.mods.back(); + + DSMModule* last_loaded = NULL; + modLinkHdl modHdl = preload_reader.mods.back(); + + if (modHdl.mod) + last_loaded = modHdl.mod; + else + { + ret.push(500); + ret.push("Error while preloading '"+mod_name+"'"); + return; + } + if (last_loaded) { if (last_loaded->preload()) { ret.push(500); diff --git a/apps/dsm/DSMChartReader.cpp b/apps/dsm/DSMChartReader.cpp index b03d4d8f..8918e26e 100644 --- a/apps/dsm/DSMChartReader.cpp +++ b/apps/dsm/DSMChartReader.cpp @@ -119,10 +119,16 @@ string DSMChartReader::getToken(string str, size_t& pos) { } DSMAction* DSMChartReader::actionFromToken(const string& str) { - for (vector::iterator it= - mods.begin(); it!= mods.end(); it++) { - DSMAction* a = (*it)->getAction(str); - if (a) return a; + + for (v_modsHdls::iterator it=mods.begin(); it!= mods.end(); it++) + { + DSMModule* mod = (*it).mod; + if (!mod) + continue; + + DSMAction* a = mod->getAction(str); + if (a) + return a; } DSMAction* a = core_mod.getAction(str); @@ -201,9 +207,14 @@ bool DSMChartReader::forFromToken(DSMArrayFor& af, const string& token) { } DSMCondition* DSMChartReader::conditionFromToken(const string& str, bool invert) { - for (vector::iterator it= - mods.begin(); it!= mods.end(); it++) { - DSMCondition* c=(*it)->getCondition(str); + + for (v_modsHdls::iterator it=mods.begin(); it!= mods.end(); it++) + { + DSMModule* mod = (*it).mod; + if (!mod) + continue; + + DSMCondition* c = mod->getCondition(str); if (c) { c->invert = invert; return c; @@ -252,7 +263,10 @@ bool DSMChartReader::importModule(const string& mod_cmd, const string& mod_path) fname.c_str()); return false; } - mods.push_back(mod); + + modLinkHdl mod_hdl = {mod, h_dl}; + mods.push_back(mod_hdl); + DBG("loaded module '%s' from '%s'\n", params.c_str(), fname.c_str()); return true; @@ -735,16 +749,30 @@ bool DSMChartReader::decode(DSMStateDiagram* e, const string& chart, } - for (vector::iterator it= - mods.begin(); it != mods.end(); it++) - out_mods.push_back(*it); + for (v_modsHdls::iterator it=mods.begin(); it!= mods.end(); it++) + { + DSMModule* mod = (*it).mod; + if (mod) + out_mods.push_back(mod); + } return true; } void DSMChartReader::cleanup() { - for (vector::iterator it=mods.begin(); it != mods.end(); it++) - delete *it; + + for (v_modsHdls::iterator it=mods.begin(); it!= mods.end(); it++) + { + DSMModule* mod = (*it).mod; + void* h_dl = (*it).h_dl; + + if (mod) + delete mod; + + if (h_dl) + dlclose(h_dl); + } + mods.clear(); } diff --git a/apps/dsm/DSMChartReader.h b/apps/dsm/DSMChartReader.h index 34db5ba2..be726fc6 100644 --- a/apps/dsm/DSMChartReader.h +++ b/apps/dsm/DSMChartReader.h @@ -34,6 +34,12 @@ #include using std::string; +struct modLinkHdl { + DSMModule* mod; + void* h_dl; +}; +typedef vector v_modsHdls; + class NamedAction : public DSMAction { public: NamedAction(const string& m_name) { @@ -96,7 +102,9 @@ class DSMChartReader { bool forFromToken(DSMArrayFor& af, const string& token); bool importModule(const string& mod_cmd, const string& mod_path); - vector mods; + + v_modsHdls mods; + DSMCoreModule core_mod; vector funcs;