From b9dd3699081e9723a90467b2f18d696845d906c3 Mon Sep 17 00:00:00 2001 From: CeciliaAvila Date: Tue, 8 Aug 2023 12:07:02 -0300 Subject: [PATCH] Replace Map with WeakMap to avoid memory leak --- .../adaptive-expressions/src/parser/expressionParser.ts | 8 ++++---- .../src/languageGeneratorExtensions.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/adaptive-expressions/src/parser/expressionParser.ts b/libraries/adaptive-expressions/src/parser/expressionParser.ts index ce5d2d4ac9..e93fc7458b 100644 --- a/libraries/adaptive-expressions/src/parser/expressionParser.ts +++ b/libraries/adaptive-expressions/src/parser/expressionParser.ts @@ -27,7 +27,7 @@ export class ExpressionParser implements ExpressionParserInterface { */ readonly EvaluatorLookup: EvaluatorLookup; - private static expressionDict: Map = new Map(); + private static expressionDict: WeakMap = new WeakMap(); private readonly ExpressionTransformer = class extends AbstractParseTreeVisitor @@ -279,8 +279,8 @@ export class ExpressionParser implements ExpressionParserInterface { * @returns A ParseTree. */ protected static antlrParse(expression: string): ParseTree { - if (ExpressionParser.expressionDict.has(expression)) { - return ExpressionParser.expressionDict.get(expression); + if (ExpressionParser.expressionDict.has({ key: expression })) { + return ExpressionParser.expressionDict.get({ key: expression }); } const inputStream: ANTLRInputStream = new ANTLRInputStream(expression); @@ -297,7 +297,7 @@ export class ExpressionParser implements ExpressionParserInterface { if (file !== undefined) { expressionContext = file.expression(); } - ExpressionParser.expressionDict.set(expression, expressionContext); + ExpressionParser.expressionDict.set({ key: expression }, expressionContext); return expressionContext; } diff --git a/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts b/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts index d82a554a99..aac5b75e39 100644 --- a/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts +++ b/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts @@ -36,7 +36,7 @@ export const languagePolicyKey = Symbol('LanguagePolicy'); * Extension methods for language generator. */ export class LanguageGeneratorExtensions { - private static readonly _languageGeneratorManagers = new Map(); + private static readonly _languageGeneratorManagers = new WeakMap(); /** * Register default LG file or a language generator as default language generator.