Skip to content

Commit

Permalink
Merge pull request #81 from bigcat26/fix_scope_leak
Browse files Browse the repository at this point in the history
fix: memory leak
  • Loading branch information
hsluoyz authored Feb 26, 2021
2 parents b0f7c79 + a9565fc commit 8c76f28
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
9 changes: 6 additions & 3 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ Enforcer :: Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {

this->model = m;
this->model->PrintModel();
this->func_map.LoadFunctionMap();

this->Initialize();

Expand Down Expand Up @@ -468,7 +467,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 +481,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 8c76f28

Please sign in to comment.