Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix memory leak in Enforce() #246

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions include/casbin/model/exprtk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,44 +245,65 @@ class ExprtkFunctionFactory {
public:
static std::shared_ptr<exprtk_func_t> GetExprtkFunction(ExprtkFunctionType type, int narg, std::shared_ptr<RoleManager> rm = nullptr) {
std::string idenfier(narg, 'S');

// Static map to act as an object pool
static std::unordered_map<std::string, std::shared_ptr<exprtk_func_t>> pool;

// Create a key for the object pool using the type and identifier
std::string key = std::to_string(static_cast<int>(type)) + idenfier;

// Check if the object already exists in the pool
if (pool.find(key) != pool.end()) {
// If it exists, return the existing object
return pool[key];
}

// If it doesn't exist, create a new object
std::shared_ptr<exprtk_func_t> func = nullptr;
switch (type) {
case ExprtkFunctionType::Gfunction:
func = std::make_shared<ExprtkGFunction>(idenfier, rm);
break;
case ExprtkFunctionType::KeyMatch:
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch));
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch);
break;
case ExprtkFunctionType::KeyMatch2:
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch2));
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch2);
break;
case ExprtkFunctionType::KeyMatch3:
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch3));
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch3);
break;
case ExprtkFunctionType::KeyMatch4:
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch4));
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch4);
break;
case ExprtkFunctionType::IpMatch:
func.reset(new ExprtkMatchFunction(idenfier, IPMatch));
func = std::make_shared<ExprtkMatchFunction>(idenfier, IPMatch);
break;
case ExprtkFunctionType::RegexMatch:
func.reset(new ExprtkMatchFunction(idenfier, RegexMatch));
func = std::make_shared<ExprtkMatchFunction>(idenfier, RegexMatch);
break;
case ExprtkFunctionType::KeyGet:
func.reset(new ExprtkGetFunction(idenfier, KeyGet));
func = std::make_shared<ExprtkGetFunction>(idenfier, KeyGet);
break;
case ExprtkFunctionType::KeyGet2:
func.reset(new ExprtkGetWithPathFunction(idenfier, KeyGet2));
func = std::make_shared<ExprtkGetWithPathFunction>(idenfier, KeyGet2);
break;
case ExprtkFunctionType::KeyGet3:
func.reset(new ExprtkGetWithPathFunction(idenfier, KeyGet3));
func = std::make_shared<ExprtkGetWithPathFunction>(idenfier, KeyGet3);
break;
default:
func = nullptr;
}

// If a new object was created, add it to the pool
if (func) {
pool[key] = func;
}
pool.clear();
// Return the newly created or existing object
return func;
}

};
} // namespace casbin

Expand Down
Loading