Skip to content

Commit

Permalink
fix: memory leak
Browse files Browse the repository at this point in the history
Signed-off-by: chris <bigcat26@gmail.com>
  • Loading branch information
bigcat26 committed Feb 26, 2021
1 parent b0f7c79 commit 7684024
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {
PushStringPropToObject(scope, "r", params[i], r_tokens[i].substr(2, r_tokens[i].size() - 2));
}

return this->enforce(matcher, scope);
bool result = this->enforce(matcher, scope);
DeinitializeScope(scope);
return result;
}

// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
Expand All @@ -480,5 +482,7 @@ bool Enforcer::EnforceWithMatcher(string matcher, unordered_map<string, string>
PushStringPropToObject(scope, "r", r.second, r.first);
}

return this->enforce(matcher, scope);
bool result = this->enforce(matcher, scope);
DeinitializeScope(scope);
return result;
}
2 changes: 1 addition & 1 deletion casbin/model/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "../util/util.h"

FunctionMap :: FunctionMap(){
scope = InitializeScope();
scope = NULL;
}

void FunctionMap :: ProcessFunctions(string expression){
Expand Down
4 changes: 4 additions & 0 deletions casbin/model/scope_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Scope InitializeScope() {
return duk_create_heap_default();
}

void DeinitializeScope(Scope scope) {
duk_destroy_heap(scope);
}

void PushFunctionValue(Scope scope, Function f, int nargs){
duk_push_c_function(scope, f, (Index)nargs);
}
Expand Down
1 change: 1 addition & 0 deletions casbin/model/scope_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef duk_c_function Function;
typedef duk_idx_t Index;

Scope InitializeScope();
void DeinitializeScope(Scope scope);
void PushFunctionValue(Scope scope, Function f, int nargs);
void PushBooleanValue(Scope scope, bool expression);
void PushTrueValue(Scope scope);
Expand Down

0 comments on commit 7684024

Please sign in to comment.