Skip to content

Commit 9e33532

Browse files
committed
perf: fix potential memory leak for old comment block configurations.
- Added new `commentBlocksDisposables` disposable array to keep track of the comment blocks. - Changed the variable name for the comment block disposable to `commentBlocksDisposables`. - Fixed potential memory leak in the `onDidOpenTextDocument` event handler. On each document opening, we reconfigure the comment blocks configs which pushes new disposables everytime. This could lead to memory leaks over time. Fixed by looping through the `commentBlocksDisposables` array and disposing of each configuration, before new ones are created.
1 parent 3cb61c3 commit 9e33532

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/extension.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ export function activate(context: vscode.ExtensionContext) {
2424

2525
// Store disposables for cleanup
2626
const disposables: vscode.Disposable[] = [];
27-
const configureCommentBlocksDisposable = configuration.configureCommentBlocks();
28-
disposables.push(...configureCommentBlocksDisposable);
29-
30-
configuration.registerCommands(context);
27+
let commentBlocksDisposables: vscode.Disposable[] = [];
3128

29+
// Initial configuration
30+
commentBlocksDisposables = configuration.configureCommentBlocks();
31+
disposables.push(...commentBlocksDisposables);
3232

33-
let disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages");
33+
configuration.registerCommands(context);
3434

35+
// Show disabled languages message
36+
const disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages");
3537
if (disabledLangConfig.length > 0) {
3638
vscode.window.showInformationMessage(`${disabledLangConfig.join(", ")} languages are disabled for ${extensionDisplayName}.`);
3739
}
@@ -85,8 +87,14 @@ export function activate(context: vscode.ExtensionContext) {
8587
*/
8688
const documentOpenDisposable = vscode.workspace.onDidOpenTextDocument(() => {
8789
logger.info("Active editor language changed, re-configuring comment blocks.");
88-
const configureCommentBlocksDisposable = configuration.configureCommentBlocks();
89-
disposables.push(...configureCommentBlocksDisposable);
90+
91+
// Dispose of old comment block configurations to prevent memory leaks
92+
commentBlocksDisposables.forEach((disposable) => disposable.dispose());
93+
commentBlocksDisposables = [];
94+
95+
// Create new comment block configurations
96+
commentBlocksDisposables = configuration.configureCommentBlocks();
97+
disposables.push(...commentBlocksDisposables);
9098
});
9199

92100
disposables.push(documentOpenDisposable);

0 commit comments

Comments
 (0)