diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-08 20:10:48 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:49 +0100 |
commit | c4b36f01f3286a1c7f183323df00b36270c8498b (patch) | |
tree | 93bcc0c3956d161c80067cf5903f43c53be252a1 | |
parent | e599b4ba129df475a9c8bb8290c3803f9dda664a (diff) |
Fix memory leaks relative to json_object
Change-Id: I0cff16c1d44b9363522fde7c6c9b2bd10ba376ac
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | signal-composer-binding/signal-composer-binding.cpp | 3 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 17 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.hpp | 1 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 2 |
4 files changed, 20 insertions, 3 deletions
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp index 9a113d5..55c11fe 100644 --- a/signal-composer-binding/signal-composer-binding.cpp +++ b/signal-composer-binding/signal-composer-binding.cpp @@ -178,16 +178,19 @@ void loadConf(afb_req request) if( sourcesJ && composer.loadSources(sourcesJ)) { afb_req_fail_f(request, "Loading 'sources' configuration or subscription error", "Error code: -1"); + json_object_put(fileJ); return; } if(signalsJ && composer.loadSignals(signalsJ)) { afb_req_fail_f(request, "Loading 'signals' configuration or subscription error", "Error code: -1"); + json_object_put(fileJ); return; } else {composer.initSignals();} + json_object_put(fileJ); afb_req_success(request, NULL, NULL); } diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index 8461748..4a0346c 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -89,6 +89,12 @@ Composer::Composer() Composer::~Composer() { + for(auto& j: ctlActionsJ_) + { + json_object_put(j); + } + json_object_put(ctlConfig_->configJ); + json_object_put(ctlConfig_->requireJ); free(ctlConfig_); } @@ -174,7 +180,7 @@ json_object* Composer::buildLuaAction(std::string name, std::string function, js CtlActionT* Composer::convert2Action(const std::string& name, json_object* actionJ) { json_object *functionArgsJ = nullptr, - *ctlActionJ = nullptr; + *ctlActionJ = nullptr; char *function; const char *plugin; CtlActionT *ctlAction = new CtlActionT; @@ -219,10 +225,13 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio } } + // Register json object for later release + ctlActionsJ_.push_back(ctlActionJ); if(ctlActionJ) { - if(!ActionLoadOne(nullptr, ctlAction, ctlActionJ, 0)) - return ctlAction; + int err = ActionLoadOne(nullptr, ctlAction, ctlActionJ, 0); + if(! err) + {return ctlAction;} } return nullptr; @@ -306,6 +315,8 @@ int Composer::loadOneSourceAPI(json_object* sourceJ) { std::string function = "api://" + std::string(api) + "/subscribe"; getSignalsJ = buildApiAction("getSignals", function, nullptr); + // Register json object for later release + ctlActionsJ_.push_back(getSignalsJ); } getSignalsCtl = convert2Action("getSignals", getSignalsJ); diff --git a/signal-composer-binding/signal-composer.hpp b/signal-composer-binding/signal-composer.hpp index 8ae41e9..9dc80ed 100644 --- a/signal-composer-binding/signal-composer.hpp +++ b/signal-composer-binding/signal-composer.hpp @@ -26,6 +26,7 @@ private: CtlConfigT* ctlConfig_; static CtlSectionT ctlSections_[]; ///< Config Section definition (note: controls section index should match handle retrieval in) + std::vector<json_object*> ctlActionsJ_; ///< Vector of action json object to be kept if we want to freed them correctly avoiding leak mem. std::vector<std::shared_ptr<SourceAPI>> newSourcesListV_; std::vector<std::shared_ptr<SourceAPI>> sourcesListV_; diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index c09e059..b9663ee 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -75,6 +75,8 @@ Signal::Signal(const std::string& id, Signal::~Signal() { + json_object_put(onReceived_->argsJ); + json_object_put(getSignalsArgs_); delete(signalCtx_); delete(onReceived_); } |