Skip to content

Commit 8c76f28

Browse files
authored
Merge pull request #81 from bigcat26/fix_scope_leak
fix: memory leak
2 parents b0f7c79 + a9565fc commit 8c76f28

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

casbin/enforcer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ Enforcer :: Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
200200

201201
this->model = m;
202202
this->model->PrintModel();
203-
this->func_map.LoadFunctionMap();
204203

205204
this->Initialize();
206205

@@ -468,7 +467,9 @@ bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {
468467
PushStringPropToObject(scope, "r", params[i], r_tokens[i].substr(2, r_tokens[i].size() - 2));
469468
}
470469

471-
return this->enforce(matcher, scope);
470+
bool result = this->enforce(matcher, scope);
471+
DeinitializeScope(scope);
472+
return result;
472473
}
473474

474475
// 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 "".
@@ -480,5 +481,7 @@ bool Enforcer::EnforceWithMatcher(string matcher, unordered_map<string, string>
480481
PushStringPropToObject(scope, "r", r.second, r.first);
481482
}
482483

483-
return this->enforce(matcher, scope);
484+
bool result = this->enforce(matcher, scope);
485+
DeinitializeScope(scope);
486+
return result;
484487
}

casbin/model/function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "../util/util.h"
2323

2424
FunctionMap :: FunctionMap(){
25-
scope = InitializeScope();
25+
scope = NULL;
2626
}
2727

2828
void FunctionMap :: ProcessFunctions(string expression){

casbin/model/scope_config.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Scope InitializeScope() {
2424
return duk_create_heap_default();
2525
}
2626

27+
void DeinitializeScope(Scope scope) {
28+
duk_destroy_heap(scope);
29+
}
30+
2731
void PushFunctionValue(Scope scope, Function f, int nargs){
2832
duk_push_c_function(scope, f, (Index)nargs);
2933
}

casbin/model/scope_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef duk_c_function Function;
4040
typedef duk_idx_t Index;
4141

4242
Scope InitializeScope();
43+
void DeinitializeScope(Scope scope);
4344
void PushFunctionValue(Scope scope, Function f, int nargs);
4445
void PushBooleanValue(Scope scope, bool expression);
4546
void PushTrueValue(Scope scope);

0 commit comments

Comments
 (0)