fix: DynamicMenuWidget Memory Leak #15975 #15982
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What it does
Fixes a memory leak in @theia/core/lib/browser/browser-menu-plugin.ts caused by inconsistent usage of addEventListener and removeEventListener.
Previously, an event listener was registered with capture: true, but removed without specifying the same option.
This mismatch prevented the listener from being properly removed, leading to retained memory on repeated context menu interactions.
This change ensures that both addEventListener and removeEventListener use the same options, allowing proper cleanup and stable memory usage.
Fixes #15975
How to test
Launch Theia and open the browser developer tools (e.g., Chrome DevTools).
Right-click anywhere in the Theia UI to open the context menu, then click elsewhere to close it.
Repeat the open/close operation ~10 times.
Take two memory snapshots before and after.
You should now see that no redundant event listeners remain and memory usage stays stable.
Without the fix, you would observe increasing retained nodes in memory.
Follow-ups
None currently.
Could consider writing automated regression tests if event listener state becomes more observable in future refactoring.
Breaking changes
This PR does not introduce breaking changes.
Attribution
Contributed by Hbb.