Skip to content

Commit 5f01444

Browse files
authored
feat: fix memory leak in Enforce() (#246)
1 parent 8c5c7c4 commit 5f01444

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

include/casbin/model/exprtk_config.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,44 +245,65 @@ class ExprtkFunctionFactory {
245245
public:
246246
static std::shared_ptr<exprtk_func_t> GetExprtkFunction(ExprtkFunctionType type, int narg, std::shared_ptr<RoleManager> rm = nullptr) {
247247
std::string idenfier(narg, 'S');
248+
249+
// Static map to act as an object pool
250+
static std::unordered_map<std::string, std::shared_ptr<exprtk_func_t>> pool;
251+
252+
// Create a key for the object pool using the type and identifier
253+
std::string key = std::to_string(static_cast<int>(type)) + idenfier;
254+
255+
// Check if the object already exists in the pool
256+
if (pool.find(key) != pool.end()) {
257+
// If it exists, return the existing object
258+
return pool[key];
259+
}
260+
261+
// If it doesn't exist, create a new object
248262
std::shared_ptr<exprtk_func_t> func = nullptr;
249263
switch (type) {
250264
case ExprtkFunctionType::Gfunction:
251265
func = std::make_shared<ExprtkGFunction>(idenfier, rm);
252266
break;
253267
case ExprtkFunctionType::KeyMatch:
254-
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch));
268+
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch);
255269
break;
256270
case ExprtkFunctionType::KeyMatch2:
257-
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch2));
271+
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch2);
258272
break;
259273
case ExprtkFunctionType::KeyMatch3:
260-
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch3));
274+
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch3);
261275
break;
262276
case ExprtkFunctionType::KeyMatch4:
263-
func.reset(new ExprtkMatchFunction(idenfier, KeyMatch4));
277+
func = std::make_shared<ExprtkMatchFunction>(idenfier, KeyMatch4);
264278
break;
265279
case ExprtkFunctionType::IpMatch:
266-
func.reset(new ExprtkMatchFunction(idenfier, IPMatch));
280+
func = std::make_shared<ExprtkMatchFunction>(idenfier, IPMatch);
267281
break;
268282
case ExprtkFunctionType::RegexMatch:
269-
func.reset(new ExprtkMatchFunction(idenfier, RegexMatch));
283+
func = std::make_shared<ExprtkMatchFunction>(idenfier, RegexMatch);
270284
break;
271285
case ExprtkFunctionType::KeyGet:
272-
func.reset(new ExprtkGetFunction(idenfier, KeyGet));
286+
func = std::make_shared<ExprtkGetFunction>(idenfier, KeyGet);
273287
break;
274288
case ExprtkFunctionType::KeyGet2:
275-
func.reset(new ExprtkGetWithPathFunction(idenfier, KeyGet2));
289+
func = std::make_shared<ExprtkGetWithPathFunction>(idenfier, KeyGet2);
276290
break;
277291
case ExprtkFunctionType::KeyGet3:
278-
func.reset(new ExprtkGetWithPathFunction(idenfier, KeyGet3));
292+
func = std::make_shared<ExprtkGetWithPathFunction>(idenfier, KeyGet3);
279293
break;
280294
default:
281295
func = nullptr;
282296
}
283297

298+
// If a new object was created, add it to the pool
299+
if (func) {
300+
pool[key] = func;
301+
}
302+
pool.clear();
303+
// Return the newly created or existing object
284304
return func;
285305
}
306+
286307
};
287308
} // namespace casbin
288309

0 commit comments

Comments
 (0)